C # synchronize SQL Server database Schema,

Source: Internet
Author: User

C # synchronize SQL Server database Schema,
C # synchronize SQL Server database Schema1. write an SQL processing class first:

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. Write another database type class:
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. Write a schema for the table structure of the synchronization database:

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. Finally, execute the synchronization function:
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());            }        }

Note:This is just a simple DB schema synchronization, and can be improved in many places, such as constraints, dual primary keys, foreign keys, and so on.


C language ^ how to use

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

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

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

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

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.