sqlserver批量更新
时间:2021-07-01 10:21:17
帮助过:2人阅读
class DBHelper
2 {
3 //操作配置文件
4 Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
5 //先取一次,做为对比的基础
6 SqlConnection conn =
new SqlConnection();
7 SqlCommand comm =
new SqlCommand();
8 //链接数据库
9 public void Open()
10 {
11 conn =
new SqlConnection(config.AppSettings.Settings[
"connstr"].Value);
12 conn.Open();
13 }
14 //断开连接
15 public void Close()
16 {
17 conn.Close();
18 }
19 //执行sql,并返回第一行第一列
20 public object ExecuteScalar(
string sql)
21 {
22 comm.CommandText =
sql;
23 comm.Connection =
conn;
24 return comm.ExecuteScalar();
25 }
26 //执行sql,并返回执行结果
27 public DataTable GetResult(
string sql)
28 {
29 SqlDataAdapter sda =
new SqlDataAdapter(sql, conn);
30 DataTable dt =
new DataTable();
31 sda.Fill(dt);
32 return dt;
33 }
34 //执行sql
35 public void ExecuteNonQuery(
string sql)
36 {
37 comm.CommandText =
sql;
38 comm.ExecuteNonQuery();
39 }
40 //批量更新
41 public void Update(DataTable dt,
string tablename)
42 {
43 using (SqlBulkCopy sqlcopy =
new SqlBulkCopy(conn))
44 {
45 sqlcopy.BulkCopyTimeout =
10000;
46 sqlcopy.DestinationTableName =
tablename;
47 sqlcopy.WriteToServer(dt);
48 }
49 }
50 }
sqlserver批量更新
标签: