惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Vercel News
Vercel News
月光博客
月光博客
S
SegmentFault 最新的问题
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
U
Unit 42
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
小众软件
小众软件
WordPress大学
WordPress大学
G
Google Developers Blog
Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 【当耐特】
I
InfoQ

博客园 - 放飞梦想

SQL SERVER数据库表结构对比 无法识别的配置节点 applicationSettings/* Properties.Settings 解决方法 删除重复数据只保留一条 SQL语句的事务处理及错误处理结构 SQLServer查询执行过的SQL记录 软件的自动更新实现 国家气象局提供的天气预报接口 SQL SERVER 2005 数据库状态为“可疑”的解决方法 Oracle 递归查询子节点和父节点 SQLite初体验 两张表数据同步用触发器 清理数据库日志 Sqlserver数据库还原一直显示“正在还原…”解决方法 如何查看 SQL Server 执行的历史 SQL 语句记录? MYSQL 命令行大全 (简洁、明了、全面) MongoDB下载页面 等级保护和分级保护区别与联系 等级保护和分级保护 SQL2005EXPress自动备份
C#Datatable导入sqlserver数据库中,三种常见,快捷的方法
放飞梦想 · 2020-02-26 · via 博客园 - 放飞梦想

1,最常见的Executenonquery(返回影响的行数)sql是我的查询插入语句,你可以换成你的!这种方式入库,速度一般,大量数据时不提倡使用

 1         /// <summary>
 2         /// 通过table一个一个的插入
 3         /// </summary>
 4         /// <param name="table"></param>
 5         public static void Executenonquery(DataTable table)
 6         {
 7             foreach (DataRow itemRow in table.Rows)
 8             {
 9                 //if exists(select * from dbo.ID where ENG = '')
10                 //   begin
11                 //        return;
12                 //                end
13                 //else
14                 //   begin
15                 //        INSERT INTO ID([ENG],[GB],[B5],[FILE],[MSG]) values('', '', '', '', '')
16                 //   end
17                 string sql = "if exists(select * from " + table.TableName + " where ENG = '" + itemRow["ENG"].ToString() + "') "+
18                              " begin return; end else begin INSERT INTO " + table.TableName + "([ENG],[GB],[B5],[FILE],[MSG])" +
19                              "VALUES('" + itemRow["ENG"].ToString() + "'" +
20                              ",'" + itemRow["GB"].ToString() + "'" +
21                              ",'" + itemRow["B5"].ToString() + "'" +
22                              ",'" + itemRow["FILE"].ToString() + "'" +
23                              ",'" + itemRow["MSG"].ToString() + "') end";
24                 using (SqlConnection sqlconn = new SqlConnection(connectString))
25                 {
26                     sqlconn.Open();
27  
28                     SqlCommand sqlcommand = new SqlCommand(sql, sqlconn);
29                     sqlcommand.ExecuteNonQuery();
30                     sqlconn.Close();
31                 }
32             }
33         }
34 ————————————————

View Code

2,通过adapter入库,这种入库,起先,你需要先把datatable放入到dataset中然后进行入库,这种方式主要是对库中对应的表进行增删改,方便使用(效率只比第一种方式好点)

 1        /// <summary>
 2         /// 通过adapter更新数据库
 3         /// </summary>
 4         /// <param name="dataset"></param>
 5         public static void DataadapterInssert(DataSet dataset)
 6         {
 7             if (dataset.Tables.Count > 0)
 8             {
 9                 foreach (DataTable itemTable in dataset.Tables)
10                 {
11                     SqlCommand insertcommand = new SqlCommand("if exists(select * from " + itemTable.TableName + " where ENG = @ENG) begin return; end "+
12                                       " else begin INSERT INTO " + itemTable.TableName + "([ENG],[GB],[B5],[FILE],[MSG])" +
13                                       "VALUES(@ENG, @GB,@B5,@FILE,@MSG) end", new SqlConnection(connectString));
14                     insertcommand.Parameters.Add("@ENG", SqlDbType.VarChar, 100, "ENG");
15                     insertcommand.Parameters.Add("@GB", SqlDbType.VarChar, 100, "GB");
16                     insertcommand.Parameters.Add("@B5", SqlDbType.VarChar, 200, "B5");
17                     insertcommand.Parameters.Add("@FILE", SqlDbType.VarChar, 200, "FILE");
18                     insertcommand.Parameters.Add("@MSG", SqlDbType.VarChar, 100, "MSG");
19  
20                     SqlDataAdapter sqldataadapter = new SqlDataAdapter();
21                     sqldataadapter.InsertCommand = insertcommand;
22  
23                     sqldataadapter.Update(dataset, itemTable.TableName);
24                 }
25             }
26             
27         }

View Code

3,重头戏都在最后,这种方式速度比前两种快很多,适合用于大量数据插入更新,也将datatable放入dataset中然后通过遍历,将datatable复制到数据库中对应的表中,快速便捷

 1         /// <summary>
 2         /// 通过SqlBulkCopy复制table数据到数据库
 3         /// </summary>
 4         /// <param name="dataset"></param>
 5         public static void SqlbulkcopyInsert(DataSet dataset)
 6         {
 7             string ie;
 8             if (dataset.Tables.Count > 0)
 9             {
10                 foreach (DataTable itemTable in dataset.Tables)
11                 {
12                     // SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectString, SqlBulkCopyOptions.KeepIdentity);//删除自增ID插入原始数据
                    SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectString, SqlBulkCopyOptions.UseInternalTransaction);//批量事务处理
13 sqlbulkcopy.DestinationTableName = itemTable.TableName;//数据库中的表名 14 for (int i = 0; i < itemTable.Rows.Count; i++) 15 { 16 ie = itemTable.Rows[i][2].ToString(); 17 } 18 sqlbulkcopy.WriteToServer(itemTable); 19 } 20 } 21 22 }

版权声明:本文为CSDN博主「丘鸣山RM」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zyzBulus/article/details/77479272