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; } }