SqlBulkCopy类 基本使用方法

View Code
 1  private void button1_Click(object sender, EventArgs e)
2 {
3 try
4 {
5 string DatabaseOne = "Data Source = DATABASE-VPC\\SQL2005; User ID = sa; password = admin; Initial Catalog = AdventureWorks; Pooling=true";
6 string DatabaseTwo = "Data Source = DATABASE-VPC\\SQL2005; User ID = sa; password = admin; Initial Catalog = Jiang; Pooling=true";
7 SqlConnection Conn = new SqlConnection(DatabaseOne);
8 SqlCommand cmd = new SqlCommand();
9 cmd.Connection = Conn;
10 cmd.CommandType = CommandType.Text;
11 cmd.CommandText = "select * from TableCopyTest";
12 Conn.Open();
13 DataSet ds = new DataSet();
14 SqlDataAdapter per = new SqlDataAdapter(cmd);
15 per.Fill(ds, "One");
16 cmd.ExecuteNonQuery();
17 SqlBulkCopy copytable = new SqlBulkCopy(DatabaseTwo, SqlBulkCopyOptions.UseInternalTransaction);
18 copytable.BulkCopyTimeout = 500000;
19 copytable.SqlRowsCopied += new SqlRowsCopiedEventHandler(copytable_SqlRowsCopied);
20 copytable.NotifyAfter = ds.Tables[0].Rows.Count;
21
22 copytable.DestinationTableName = "TableCopyTwo";
23 copytable.ColumnMappings.Add("uid", "pass");///互换对应列
24 copytable.WriteToServer(ds.Tables[0]);
25 }
26 catch (Exception ex) { MessageBox.Show(ex.Message); }
27 }
28
29 void copytable_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
30 {
31 this.label1.Text = "完成! 共:" + e.RowsCopied.ToString();
32 }

  

你可能感兴趣的:(copy)