C # Update SQL Server database notes information from another database

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.