public class Sqlbulkcopyclass {public string sqlconstring = "Data source=.;i Nitial Catalog=testdb; User Id=sa; password=123456789 "; public void FindAll () {try {using (SqlConnection Sqlcon = new SqlConnection ( sqlconstring)) {Sqlcon. Open (); SqlCommand com = new SqlCommand (); String sql = "SELECT [Id],[name] from [TestDB]. [dbo]. [TestTable] "; Com.commandtext = SQL; Com.commandtype = CommandType.Text; Com. Connection = Sqlcon; SqlDataReader reader = com. ExecuteReader (); using (SqlConnection Sqlcon2 = new SqlConnection (sqlconstring)) {Sqlcon2. Open (); using (SqlBulkCopy bulkcopy = new SqlBulkCopy (sqlcon2)) {bulkcopy. DestinationTableName = "TestTable2"; try {bulkcopy. WriteToServer (reader); } catch (Exception ex) {throw ex; } finally { Reader. Close (); }}}}} catch (Exception ex) { Throw ex; } } }
Copy table contents (table structure is the same)