usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;usingSystem.Diagnostics;namespacesyncdatabasecolumndescription{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, EventArgs e) { varSSB =NewSqlConnectionStringBuilder (); Ssb. DataSource=@". \sql2008"; Ssb. UserID="SA"; Ssb. Password="Firstelite"; Ssb. InitialCatalog="Omsdb"; using(varConnection =NewSqlConnection (SSB. ConnectionString)) {connection. Open (); varTablelist =gettablelist (connection); foreach(varTableinchtablelist) { varFieldList =getfieldlist (connection, table); foreach(varFieldinchFieldList) { vardesc =getfielddescription (Connection, table, field); if(Desc! =NULL) Debug.Print (Table+" -"+ Field +":"+desc); } } } } Private voidupdatefielddescription () {varSSB =NewSqlConnectionStringBuilder (); Ssb. DataSource=@". \sql2008"; Ssb. UserID="**"; Ssb. Password="****"; Ssb. InitialCatalog="Omsdb_with_memo"; using(varConnection =NewSqlConnection (SSB. ConnectionString)) {connection. Open (); varTablelist =gettablelist (connection); Ssb. InitialCatalog="Omsdb"; using(varConnection2 =NewSqlConnection (SSB. ConnectionString)) {Connection2. Open (); varTableList2 =gettablelist (Connection2); foreach(varTableinchtablelist) { if(!tablelist2.any (t = = T = =table)) Continue; varFieldList =getfieldlist (connection, table); varFieldList2 =getfieldlist (Connection2, table); foreach(varFieldinchFieldList) { if(!fieldlist2.any (f = f = =field)) Continue; vardesc =getfielddescription (Connection, table, field); if(string. Isnullorwhitespace (DESC))Continue; varDESC2 =getfielddescription (Connection2, table, field); if(DESC2! =NULL) updatefielddescription (Connection2, table, field, DESC); Elseaddfielddescription (Connection2, table, field, DESC); } } } } } Privatelist<string>gettablelist (SqlConnection connection) {varTablelist =Newlist<string>(); using(varcmd =NewSqlCommand (@"SELECT table_name from INFORMATION_SCHEMA. TABLES WHERE table_type = ' BASE TABLE ' ORDER by table_name", connection)) { using(varReader =cmd. ExecuteReader ()) { while(reader. Read ()) {Tablelist.add (reader[0]. ToString ()); } } } returntablelist; } Privatelist<string> getfieldlist (SqlConnection Connection,stringtableName) { varFieldList =Newlist<string>(); using(varcmd =NewSqlCommand ("SELECT * FROM ["+ TableName +"] WHERE 1=2", connection)) { using(varReader =cmd. ExecuteReader ()) { for(vari =0; I < reader. FieldCount; i++) Fieldlist.add (reader. GetName (i)); } } returnFieldList; } Private stringGetfielddescription (SqlConnection connection,stringTableName,stringfieldName) { using(varcmd =NewSqlCommand (@"SELECT G.[value] from Dbo.syscolumns A INNER JOIN dbo.sysobjects D on a.id = d.id and D.xtype = ' U ' and D.name <> ' dtproperties ' INNER JOIN Sys.extended_pro Perties G on a.id = g.major_id and a.colid = g.minor_id WHERE d.name = '"+ TableName +@"' and A.name= '"+ FieldName +@"'", connection)) { varRET =cmd. ExecuteScalar (); if(ret = =NULL) return NULL; returnret. ToString (); } } Private BOOLAddfielddescription (SqlConnection connection,stringTableName,stringFiledname,stringdescription) { using(varcmd =NewSqlCommand (@"EXECUTE sp_addextendedproperty N ' ms_description ', '"+ Description. Replace ("'",""'") +"', n ' user ', n ' dbo ', n ' table ', n '"+ TableName +"', n ' column ', n '"+ Filedname +"'", connection)) { returnCmd. ExecuteNonQuery () >0; } } Private BOOLUpdatefielddescription (SqlConnection connection,stringTableName,stringFiledname,stringdescription) { using(varcmd =NewSqlCommand (@"EXECUTE sp_updateextendedproperty N ' ms_description ', '"+ Description. Replace ("'",""'") +"', n ' user ', n ' dbo ', n ' table ', n '"+ TableName +"', n ' column ', n '"+ Filedname +"'", connection)) { returnCmd. ExecuteNonQuery () >0; } } }}
C # Update SQL Server database notes information from another database