C#拷贝一个库的表到另外一个库中

时间:2021-11-26 07:43:19
       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);
        }
C#拷贝一个库的表到另外一个库中C#拷贝一个库的表到另外一个库中
       /// <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