日期:2014-05-18 浏览次数:20877 次
static void Main(string[] args) { SqlConnection con = new SqlConnection("connection string "); SqlDataAdapter adapter = new SqlDataAdapter("select top 2 * from dst1",con); //从表1中查询 DataSet ds = new DataSet(); adapter.Fill(ds,"sourceTable"); DataRowCollection rowCollection = ds.Tables[0].Rows; //行集合 SqlDataAdapter adapter2 = new SqlDataAdapter("select * from dst2",con); //从表2中查询 DataSet ds2 = new DataSet(); adapter2.Fill(ds2,"targetTable"); SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter2); adapter2.InsertCommand = cmdBuilder.GetInsertCommand(); foreach (DataRow dr in rowCollection) { DataRow newRow = ds2.Tables[0].NewRow(); newRow.ItemArray = (Object[])dr.ItemArray.Clone(); ds2.Tables[0].Rows.Add(newRow); } adapter2.Update(ds2, "targetTable"); Console.ReadLine(); }