C#同步SQL Server資料庫Schema,

來源:互聯網
上載者:User

C#同步SQL Server資料庫Schema,
C#同步SQL Server資料庫Schema1. 先寫個sql處理類:

using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Text;namespace PinkDatabaseSync{    class DBUtility : IDisposable    {        private string Server;        private string Database;        private string Uid;        private string Password;        private string connectionStr;        private SqlConnection sqlConn;        public void EnsureConnectionIsOpen()        {            if (sqlConn == null)            {                sqlConn = new SqlConnection(this.connectionStr);                sqlConn.Open();            }            else if (sqlConn.State == ConnectionState.Closed)            {                sqlConn.Open();            }        }        public DBUtility(string server, string database, string uid, string password)        {            this.Server = server;            this.Database = database;            this.Uid = uid;            this.Password = password;            this.connectionStr = "Server=" + this.Server + ";Database=" + this.Database + ";User Id=" + this.Uid + ";Password=" + this.Password;        }        public int ExecuteNonQueryForMultipleScripts(string sqlStr)        {            EnsureConnectionIsOpen();            SqlCommand cmd = sqlConn.CreateCommand();            cmd.CommandType = CommandType.Text;            cmd.CommandText = sqlStr;            return cmd.ExecuteNonQuery();        }        public int ExecuteNonQuery(string sqlStr)        {            EnsureConnectionIsOpen();            SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);            cmd.CommandType = CommandType.Text;            return cmd.ExecuteNonQuery();        }        public object ExecuteScalar(string sqlStr)        {            EnsureConnectionIsOpen();            SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);            cmd.CommandType = CommandType.Text;            return cmd.ExecuteScalar();        }        public DataSet ExecuteDS(string sqlStr)        {            DataSet ds = new DataSet();            EnsureConnectionIsOpen();            SqlDataAdapter sda= new SqlDataAdapter(sqlStr,sqlConn);            sda.Fill(ds);            return ds;        }        public void Dispose()        {            if (sqlConn != null)                sqlConn.Close();        }    }}

2. 再寫個資料庫類型類:
using System;using System.Collections.Generic;using System.Text;namespace PinkDatabaseSync{    public class SQLDBSystemType    {        public static Dictionary<string, string> systemTypeDict        {            get{            var systemTypeDict = new Dictionary<string, string>();            systemTypeDict.Add("34", "image");            systemTypeDict.Add("35", "text");            systemTypeDict.Add("36", "uniqueidentifier");            systemTypeDict.Add("40", "date");            systemTypeDict.Add("41", "time");            systemTypeDict.Add("42", "datetime2");            systemTypeDict.Add("43", "datetimeoffset");            systemTypeDict.Add("48", "tinyint");            systemTypeDict.Add("52", "smallint");            systemTypeDict.Add("56", "int");            systemTypeDict.Add("58", "smalldatetime");            systemTypeDict.Add("59", "real");            systemTypeDict.Add("60", "money");            systemTypeDict.Add("61", "datetime");            systemTypeDict.Add("62", "float");            systemTypeDict.Add("98", "sql_variant");            systemTypeDict.Add("99", "ntext");            systemTypeDict.Add("104", "bit");            systemTypeDict.Add("106", "decimal");            systemTypeDict.Add("108", "numeric");            systemTypeDict.Add("122", "smallmoney");            systemTypeDict.Add("127", "bigint");            systemTypeDict.Add("240-128", "hierarchyid");            systemTypeDict.Add("240-129", "geometry");            systemTypeDict.Add("240-130", "geography");            systemTypeDict.Add("165", "varbinary");            systemTypeDict.Add("167", "varchar");            systemTypeDict.Add("173", "binary");            systemTypeDict.Add("175", "char");            systemTypeDict.Add("189", "timestamp");            systemTypeDict.Add("231", "nvarchar");            systemTypeDict.Add("239", "nchar");            systemTypeDict.Add("241", "xml");            systemTypeDict.Add("231-256", "sysname");            return systemTypeDict;            }        }    }}


 

3. 寫個同步資料庫表結構schema:

public void SyncDBSchema(string server, string dbname, string uid, string password,            string server2, string dbname2, string uid2, string password2)        {            DBUtility db = new DBUtility(server, dbname, uid, password);            DataSet ds = db.ExecuteDS("SELECT sobjects.name FROM sysobjects sobjects WHERE sobjects.xtype = 'U'");            DataRowCollection drc = ds.Tables[0].Rows;            string test = string.Empty;            string newLine = " ";            foreach (DataRow dr in drc)            {                string tableName = dr[0].ToString();                test += "if NOT exists (select * from sys.objects where name = '" + tableName + "' and type = 'u')";                test += "CREATE TABLE [dbo].[" + tableName + "](" + newLine;                DataSet ds2 = db.ExecuteDS("SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('dbo." + tableName + "')");                DataRowCollection drc2 = ds2.Tables[0].Rows;                foreach (DataRow dr2 in drc2)                {                    test += "[" + dr2["name"].ToString() + "] ";                    string typeName = SQLDBSystemType.systemTypeDict[dr2["system_type_id"].ToString()];                    test += "[" + typeName + "]";                    string charLength = string.Empty;                    if (typeName.Contains("char"))                    {                        charLength = (Convert.ToInt32(dr2["max_length"].ToString()) / 2).ToString();                        test += "(" + charLength + ")" + newLine;                    }                    bool isIdentity = bool.Parse(dr2["is_identity"].ToString());                    test += isIdentity ? " IDENTITY(1,1)" : string.Empty;                    bool isNullAble = bool.Parse(dr2["is_nullable"].ToString());                    test += (isNullAble ? " NULL," : " NOT NULL,") + newLine;                }                test += "CONSTRAINT [PK_" + tableName + "] PRIMARY KEY CLUSTERED ";                string primaryKeyName = drc2[0]["name"].ToString();                test += @"(                    [" + primaryKeyName + @"] ASC                        )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]                        ) ON [PRIMARY]" + newLine;            }            test = "use [" + dbname2 + "]" + newLine + test;            DBUtility db2 = new DBUtility(server2, dbname2, uid2, password2);            db2.ExecuteNonQueryForMultipleScripts(test);        }

4. 最後執行同步函數:
private void SyncDB_Click(object sender, EventArgs e)        {            string server = "localhost";            string dbname = "testdb1";            string uid = "sa";            string password = "password1";            string server2 = "servername2";            string dbname2 = "testdb2";            string uid2 = "sa";            string password2 = "password2";            try            {                SyncDBSchema(server, dbname, uid, password, server2, dbname2, uid2, password2);                MessageBox.Show("Done sync db schema successfully!");            }            catch (Exception exc)            {                MessageBox.Show(exc.ToString());            }        }

註: 這隻是做個簡單的DB schema同步,還可以很多地方可以繼續完善,比如約束,雙主鍵,外鍵等等。


C語言中 ^怎使用

a1 = 0x01; //0000 0001
a2 = 0x00; //0000 0000
a3 = 0x03; //0000 0011
a4 = 0x02; //0000 0010

b1 = a1 ^ a2; //0000 0001
b2 = a1 ^ a3; //0000 0010
b3 = a1 ^ a4; //0000 0011

^異或運算子,位值相同為0,不同為1,見上樣本.

//
簡單實際問題舉例:
======\=======\=======
======a=======b=======
上面是2條電路,2個開關分別為a和b,開啟狀態:\[1],關閉狀態:/[0].
若同時開啟或者關閉,兩條電路均不通.
若a開啟[1],b關閉[0],電路1通電
======\=======/=======
若a關閉[0],b開啟[1],電路2通電
======/=======\=======
綜上,電路在a,b狀態相同時不通[0],在a,b不同時通電[1].
 
C語言中 ^怎使用

a1 = 0x01; //0000 0001
a2 = 0x00; //0000 0000
a3 = 0x03; //0000 0011
a4 = 0x02; //0000 0010

b1 = a1 ^ a2; //0000 0001
b2 = a1 ^ a3; //0000 0010
b3 = a1 ^ a4; //0000 0011

^異或運算子,位值相同為0,不同為1,見上樣本.

//
簡單實際問題舉例:
======\=======\=======
======a=======b=======
上面是2條電路,2個開關分別為a和b,開啟狀態:\[1],關閉狀態:/[0].
若同時開啟或者關閉,兩條電路均不通.
若a開啟[1],b關閉[0],電路1通電
======\=======/=======
若a關閉[0],b開啟[1],電路2通電
======/=======\=======
綜上,電路在a,b狀態相同時不通[0],在a,b不同時通電[1].
 

相關文章

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.