首页 > 代码库 > 复制表内容(表结构相同)
复制表内容(表结构相同)
public class sqlbulkcopyClass { public string sqlconString = "Data Source=.;Initial 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; } } }
复制表内容(表结构相同)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。