sqlserver批量更新
写过批量更新的代码,为了⽅便查,发上来
1class DBHelper
2 {
3//操作配置⽂件
4 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 5//先取⼀次,做为对⽐的基础
6 SqlConnection conn = new SqlConnection();
7 SqlCommand comm = new SqlCommand();
8//链接数据库
9public void Open()
10 {
11 conn = new SqlConnection(config.AppSettings.Settings["connstr"].Value);
12 conn.Open();
13 }
14//断开连接
15public void Close()
16 {
17 conn.Close();
18 }
19//执⾏sql,并返回第⼀⾏第⼀列
20public object ExecuteScalar(string sql)
21 {
22 comm.CommandText = sql;
23 comm.Connection = conn;
24return comm.ExecuteScalar();
25 }
26//执⾏sql,并返回执⾏结果
27public DataTable GetResult(string sql)
28 {
29 SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
30 DataTable dt = new DataTable();
31 sda.Fill(dt);
32return dt;
33 }
34//执⾏sql
35public void ExecuteNonQuery(string sql)
36 {
37 comm.CommandText = sql;
38 comm.ExecuteNonQuery();
39 }
批量更新sql语句40//批量更新
41public void Update(DataTable dt, string tablename)
42 {
43using (SqlBulkCopy sqlcopy = new SqlBulkCopy(conn))
44 {
45 sqlcopy.BulkCopyTimeout = 10000;
46 sqlcopy.DestinationTableName = tablename;
47 sqlcopy.WriteToServer(dt);
48 }
49 }
50 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论