C#拷貝一個庫的表到另一個庫中(的四種方式)

1.該方法 基本不能用於實際開發中 ,僅供學習參考 
public string Copy() { //要複製的表名 string table = "AAAAA"; //構造鏈接字符串 OracleConnectionStringBuilder builder1 = new OracleConnectionStringBuilder(); builder1.ConnectionString = "Data Source=192.168.0.141/orcl;User ID=MY_TP_ECDB;Password=MY_TP_ECDB;Unicode=true"; OracleConnectionStringBuilder builder2 = new OracleConnectionStringBuilder(); builder2.ConnectionString = "Data Source=192.168.0.141/orcl;User ID=BZECDB;Password=BZECDB;Unicode=true"; //調用複製數據庫函數 InsertTable(builder1.ConnectionString, builder2.ConnectionString, table); return default(string); }
       /// <summary>
        /// 將巴中商城拷貝到綿陽
        /// </summary>
        private static void InsertTable(string conString1, string conString2, string tabStr)
        {
            OracleConnection connMY = new OracleConnection();
            //connMY.ConnectionString = "Data Source=192.168.0.141/orcl;User ID=MY_TP_ECDB;Password=MY_TP_ECDB;Unicode=true"; 
            connMY.ConnectionString = conString1;
            connMY.Open();

            OracleConnection connBZ = new OracleConnection();
            connBZ.ConnectionString = conString2;
            connBZ.Open();

            //將巴中數據庫拷貝到綿陽,那麼先讀出綿陽原來的數據保存在dataSet1中
            OracleDataAdapter adapter1 = new OracleDataAdapter("select * from " + tabStr , connMY);
            DataSet dataSet1 = new DataSet();

            if (dataSet1 != null)
            {
                adapter1.Fill(dataSet1, tabStr);
            }
            OracleDataAdapter adapter2 = new OracleDataAdapter("select * from " + tabStr , connBZ);
            DataSet dataSet2 = new DataSet();//巴中數據

            OracleCommand cmd2 = new OracleCommand("select count(*) from " + tabStr, connBZ);
            Object res2 = cmd2.ExecuteScalar();

            if (res2 != null)
            {
                int nCount = Convert.ToInt32(res2.ToString());
                if (nCount == 0)
                {
                    connBZ.Close();
                    connMY.Close();
                    return;
                }
            }
            //填充DataSet2 巴中的數據
            if (dataSet2 != null)
            {
                adapter2.Fill(dataSet2, tabStr);
            }
            //複製數據 
            for (int j = 0; j < dataSet2.Tables[0].Rows.Count; j++)
            {
                dataSet1.Tables[0].LoadDataRow(dataSet2.Tables[0].Rows[j].ItemArray, false);
            } 
            //將DataSet變換顯示在與其關聯的目標數據庫 
            OracleCommandBuilder cb = new OracleCommandBuilder(adapter1);
            adapter1.Update(dataSet1, tabStr);
            cb.RefreshSchema();
            Console.WriteLine("" + tabStr + "複製成功!");
            connBZ.Close();
            connMY.Close();
        }
View Code

 2.巴中數據庫同步到綿陽.rar  該項目爲實際開發時的源碼,因兩地數據庫不對外開發,Dblink 不能使用. 個人思路是巴中放一個windowsServicehtml

綿陽放一個webservice,巴中數據庫相關表web

創建觸發器 對錶的增刪改 都記錄在一張表中City_cfjlb(觸發記錄表),windowsService根據這張表的數據調用webservice 並將數據傳遞到綿陽.綿陽數據庫也需另創建一張表,保存表名稱,巴中對應表id ,綿陽對應表id數據庫

相關觸發器代碼 以下:windows

基於Oracle觸發器 存儲過程ide

3,若是量數據庫能夠建立DBlink: 那就簡單了:函數

Oracle兩個數據庫聯合查詢,使用Oracle DBLinkpost

4.數據庫建立主從關係:博主還未用到過次解決方案~~~學習

相關文章
相關標籤/搜索