数据库间复制数据 非DTS程序实现

 1 ArrayList tableArrayList = new ArrayList();
 2                 tableArrayList.AddRange(new string[]
 3             {
 4                 "AccessProfile",
 5                 "AccessProfileLast", 
 6                 "AccessProfileRight",
 7                 "Action"
 8 });
 9  currentIdConnection = -2;
10 
11                 clientConnectionBase.ExecuteNonQuery("EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'", 30);
12                 DataSet thisDataSet;
13                 clientConnectionBase.BeginTransaction(currentIdConnection);
14                 foreach (string tableName in tableArrayList)
15                 {
16                     clientConnectionBase.ExecuteNonQuery("delete from " + tableName, ref currentIdConnection, 30);
17                     thisDataSet = serverConnectionBase.Select("select * from " + tableName, tableName);
18 
19                     if (thisDataSet.Tables[0].Rows.Count > 0)
20                     {
21                         UpdateByDataSet(thisDataSet, tableName, clientConnectionBase, currentIdConnection);
22                     }
23                 }
24                 clientConnectionBase.CommitTransaction(currentIdConnection);
25 
26 
27                 clientConnectionBase.ExecuteNonQuery("EXEC sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'", 30);
28 
29                 UpdateLastSyncTime();
30 
31                 GTSMessageBox.Show(GTS.Biz.Global.GetMessage("SynchronizeCompleted"));


复制表的代码

 public int UpdateByDataSet(DataSet ds, string strTblName, ConnectionBase thisConnectionBase, int idConnection)
        {
            try
            {
                SqlDataAdapter myAdapter = new SqlDataAdapter(@"select top 1 * from " + strTblName, thisConnectionBase.Connections[idConnection]);
                DataTable thisDataTable = new DataTable();
                myAdapter.Fill(thisDataTable);
                SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
                myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
                foreach (DataRow thisDataRow in ds.Tables[0].Rows)
                {
                    DataRow currentRow = thisDataTable.NewRow();

                    //currentRow=thisDataRow;
                    foreach (DataColumn thisColumn in ds.Tables[0].Columns)
                    {
                        currentRow[thisColumn.ColumnName] = thisDataRow[thisColumn.ColumnName];
                    }
                    thisDataTable.Rows.Add(currentRow);

                }
                myAdapter.Update(thisDataTable);
                return 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

 

转载于:https://www.cnblogs.com/iven9/archive/2012/07/03/2574621.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值