⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 This program is compare the data in sql server. And you can syncronize the table and views this is v
💻 CS
📖 第 1 页 / 共 5 页
字号:

        public string[] parcala(string str)
        {
            while (!str.StartsWith("CREATE VIEW"))
            {
                str = str.Remove(0, 1);
            }
            str = str.Replace('\n', ' ');
            str = str.Replace('\r', ' ');
            int say = 0;
            int ss = str.Length;
            for (int i = 0; i < ss; i++)
            {
                if ((say == 1) && (str[i] == ' '))
                {
                    str = str.Remove(i, 1);
                    ss--;
                }
                else if ((say == 0) && (str[i]==' '))
                {
                    say = 1;
                }
                else if (str[i] != ' ')
                {
                    say = 0;
                }
            }
            str = str.Remove(0,str.IndexOf("AS SELECT")+12);
            str = str.Remove(str.IndexOf("FROM"));
            string[] dizis = str.Split(',');
            for (int i = 0; i < dizis.Length; i++)
            {
                if (dizis[i].IndexOf(" AS ") >= 0)
                {
                    dizis[i] = dizis[i].Remove(dizis[i].IndexOf(" AS "));
                }
                dizis[i] = dizis[i].Trim();
            }
            return dizis;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox2.Text += DateTime.Now.ToString() + "  --> Server1 Name = " + server1 + "  Server2 Name = " + server2 + '\n';
            richTextBox2.Text += DateTime.Now.ToString() + "  --> " + database1 + " and " + database2 + " are compare by tables and views with columns" + '\n';
            server1 = comboBox1.Text;
            server2 = comboBox3.Text;
            try
            {
                radioButton5.Checked = true;
                button6.Enabled = true;
                dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView1.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView1.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView1.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridView2.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
                button1.Enabled = true;
                adi1 = new string[20];
                kolon1 = new string[20, 20];
                tipi1 = new string[20, 20];
                uzunluk1 = new string[20, 20];
                prikey1 = new string[20];
                adi2 = new string[20];
                kolon2 = new string[20, 20];
                tipi2 = new string[20, 20];
                uzunluk2 = new string[20, 20];
                prikey2 = new string[20];
                nokey = new int[20];
                keyset = new int[20];
                tip1 = new int[20];
                tip2 = new int[20];
                check1 = new bool[20, 20];
                where = new string[20]; 

                for (int g = 0; g < 20; g++)
                {
                    tip1[g] = -1;
                    tip2[g] = -1;
                }
                dataGridView2.Rows.Clear();
                tabControl3.TabPages.Clear();
                tabControl4.TabPages.Clear();
                DataTable dt = new DataTable();
                dt = sqlcon.sqlsorgu("SELECT name FROM sysobjects WHERE xtype = 'u' and name<> 'dtproperties'", connect(server1, comboBox2.Text));
                DataTable dt1 = new DataTable();
                dt1 = sqlcon.sqlsorgu("SELECT name FROM sysobjects WHERE xtype = 'u' and name<> 'dtproperties'", connect(server2, comboBox4.Text));
                
                int i = 0;

                foreach (DataRow dr in dt.Rows)
                {
                    adi1[i] = dr["name"].ToString();
                    tip1[i] = 0;
                    i++;
                }

                int m = 0;

                foreach (DataRow dr in dt1.Rows)
                {
                    adi2[m] = dr["name"].ToString();
                    tip2[m] = 0;
                    m++;
                }

                string str = "";

                for (int j = 0; j < i; j++)
                {
                    str = adi1[j];
                    dt = sqlcon.sqlsorgu(" sp_columns '" + str + "'", connect(server1, comboBox2.Text));
                    dt1 = sqlcon.sqlsorgu(" sp_pkeys '" + str + "'", connect(server1, comboBox2.Text));
                    for (int k = 0; k < dt.Rows.Count; k++)
                    {
                        kolon1[j, k] = dt.Rows[k].ItemArray[3].ToString();
                        tipi1[j, k] = dt.Rows[k].ItemArray[5].ToString();
                        uzunluk1[j, k] = dt.Rows[k].ItemArray[7].ToString();
                        //((DataGridViewComboBoxCell)(dataGridView2.Rows[j].Cells[0])).Items.Add(str1);
                    }
                    if (dt1.Rows.Count != 0)
                    {
                        prikey1[j] = dt1.Rows[0].ItemArray[3].ToString();
                    }
                }
                for (int j = 0; j < m; j++)
                {
                    str = adi2[j];
                    dt = sqlcon.sqlsorgu(" sp_columns '" + str + "'", connect(server2, comboBox4.Text));
                    dt1 = sqlcon.sqlsorgu(" sp_pkeys '" + str + "'", connect(server2, comboBox4.Text));
                    for (int k = 0; k < dt.Rows.Count; k++)
                    {
                        kolon2[j, k] = dt.Rows[k].ItemArray[3].ToString();
                        tipi2[j, k] = dt.Rows[k].ItemArray[5].ToString();
                        uzunluk2[j, k] = dt.Rows[k].ItemArray[7].ToString();
                    }
                    if (dt1.Rows.Count != 0)
                    {
                        prikey2[j] = dt1.Rows[0].ItemArray[3].ToString();
                    }
                }

                str = "SELECT name, text from sysobjects so LEFT OUTER JOIN syscomments sc  ON (so.id = sc.id) WHERE so.xtype = 'v' ";
                dt = sqlcon.sqlsorgu(str, connect(server1, database1));
                //dataGridView3.DataSource = dt;
                int y = i;

                foreach (DataRow dr in dt.Rows)
                {
                    adi1[i] = dr["name"].ToString();
                    string[] ayna = parcala(dr["text"].ToString());
                    tip1[i] = 1;
                    for (int k = 0; k < ayna.Length; k++)
                    {
                        kolon1[i, k] = ayna[k];
                    }
                    i++;
                }
                
                
                str = "SELECT name, text from sysobjects so LEFT OUTER JOIN syscomments sc  ON (so.id = sc.id) WHERE so.xtype = 'v' ";
                dt = sqlcon.sqlsorgu(str, connect(server2, database2));

                int y1 = m;
                foreach (DataRow dr in dt.Rows)
                {
                    adi2[m] = dr["name"].ToString();
                    string[] ayna = parcala(dr["text"].ToString());
                    for (int k = 0; k < ayna.Length; k++)
                    {
                        kolon2[m, k] = ayna[k];
                    }
                    tip2[m] = 1;
                    m++;
                }
                
                datagridebas();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.ToString());
            }
        }

        public void datagridebas()
        {

            for (int i = 0; i < 20; i++)
            {
                adtut[i, 0] = -1;
                adtut[i, 1] = -1;
            }

            int k = 0;
            int satir = 1;

            for (int i = 0; i < 20; i++)
            {
                if (adi1[i] == null) break;
                for (int j = 0; j < 20; j++)
                {
                    if (adi1[i] == adi2[j])
                    {
                        adtut[k, 0] = i;
                        adtut[k, 1] = j;
                        k++;
                    }
                    if (adi2[j] == null) break;
                }
            }
            for (int i = 0; i < 20; i++)
            {
                if (adi1[i] == null) break;
                DataGridViewRow dg = new DataGridViewRow();
                dataGridView2.Rows.Insert(i, dg);
                dataGridView2.Rows[i].Cells[1].Value = adi1[i];
                for (int j = 0; j < k; j++)
                {
                    if (adtut[j, 0] == i)
                    {
                        dataGridView2.Rows[i].Cells[3].Value = adi2[adtut[j, 1]];
                        if (prikey1[i] == prikey2[adtut[j, 1]])
                        {
                            dataGridView2.Rows[i].Cells[0].Value = prikey1[i];
                        }
                        else
                        {
                            dataGridView2.Rows[i].Cells[0].Value = "Set Key!";
                        }
                    }
                }
                if (tip1[i] == 0)
                {
                    dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Blue;
                }
                else
                {
                    dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Yellow;
                }
                dataGridView2.Rows[i].Cells[2].Value = true;
                satir = i;
            }
            satir++;
            int[] olmayan = new int[20];
            for (int i = 0; i < 20; i++)
            {
                int a = 0;

                if (adi2[i] == null) break;

                for (int j = 0; j < k; j++)
                {
                    if (adtut[j, 1] != i) a++;
                }

                if (a == k)
                {
                    DataGridViewRow dg = new DataGridViewRow();
                    dataGridView2.Rows.Insert(satir, dg);
                    dataGridView2.Rows[satir].Cells[3].Value = adi2[i];
                    if (tip2[i] == 0)
                    {
                        dataGridView2.Rows[satir].Cells[2].Style.BackColor = Color.Blue;
                    }
                    else
                    {
                        dataGridView2.Rows[satir].Cells[2].Style.BackColor = Color.Yellow;
                    }
                    satir++;
                    dataGridView2.Rows[i].Cells[2].Value = true;
                }
            }
            ayni = new int[20, 20, 2];
            for (int i = 0; i < 20; i++)
            {
                for (k = 0; k < 20; k++)
                {
                    ayni[i, k, 0] = -1;
                    ayni[i, k, 1] = -1;
                }
            }
            check = new bool[2, 20, 20];
            for (int i = 0; i < satir; i++)
            {
                int d1 = -1;
                int d2 = -1;
                if ((dataGridView2.Rows[i].Cells[1].Value == null) || (dataGridView2.Rows[i].Cells[3].Value == null))
                {
                    dataGridView2.Rows[i].Cells[4].Value = "No Columns for compare";
                }
                else
                {
                    string data1 = dataGridView2.Rows[i].Cells[1].Value.ToString();
                    string data2 = dataGridView2.Rows[i].Cells[3].Value.ToString();
                    d1 = i;
                    for (int j = 0; j < 20; j++)
                    {
                        if (adi2[j] == null) break;
                        if (adi2[j] == data2)
                        {
                            d2 = j;
                            break;
                        }
                    }

                    int sayac = 0;
                    int satir1 = 0;
                    for (int j = 0; j < 20; j++)
                    {
                        int kont = sayac;
                        if (kolon1[d1, j] == null) break;
                        for (k = 0; k < 20; k++)
                        {
                            if (kolon2[d2, k] == null) break;
                            if ((kolon1[d1, j] == kolon2[d2, k]) && (prikey1[d1] == prikey2[d2]) && (tipi1[d1, j] == tipi2[d2, k])) //&& (uzunluk1[d1, j] == uzunluk2[d2, k]))
                            {
                                ayni[i, sayac, 0] = j;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -