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

推荐订阅源

Forbes - Security
Forbes - Security
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
S
Securelist
T
Tor Project blog
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
L
LINUX DO - 热门话题
V
Vulnerabilities – Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tenable Blog
博客园_首页
TaoSecurity Blog
TaoSecurity Blog
Attack and Defense Labs
Attack and Defense Labs
Project Zero
Project Zero
The Hacker News
The Hacker News
M
MIT News - Artificial intelligence
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
F
Full Disclosure
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
N
Netflix TechBlog - Medium
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
GbyAI
GbyAI
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
博客园 - 叶小钗
Webroot Blog
Webroot Blog
Hacker News: Ask HN
Hacker News: Ask HN

博客园 - 追风浪子

构建第一个Spring Boot2.0应用之集成dubbo上---环境搭建(九) linux修改系统时间为北京时间(CentOS) 构建第一个Spring Boot2.0应用之application.properties和application.yml(八) 构建第一个Spring Boot2.0应用之集成mybatis、Druid(七) 构建第一个Spring Boot2.0应用之集成mybatis(六) 构建第一个Spring Boot2.0应用之RequestMapping(四) 构建第一个Spring Boot2.0应用之Controller(三) 构建第一个spring boot2.0应用之项目启动运行的几种方式(二) 构建第一个Spring Boot2.0应用之项目创建(一) C++ 强制类型转换(转载) 【Android】ContentProvider android图片缩放平移 Android实现程序前后台切换效果 android 调试卡在:Waiting for Debugger - Application XXX is waiting for the debugger to Attach" 解决方法 android adb shell 命令大全 三种方法实现Javascript控制ScrollBar(滚动条) 从零开始学习jQuery之你必须知道的JavaScript 快速判断JavaScript对象是否存在的十个方法 jQuery插件
将DataTable 数据插入 SQL SERVER 数据库
追风浪子 · 2013-07-30 · via 博客园 - 追风浪子

以下提供3中方式将DataTable中的数据插入到SQL SERVER 数据库:

一:使用sqlcommand.executenonquery()方法插入

foreach (DataRow datarow in datatable.Rows)
{
string sql = "INSERT INTO [Table_1]

([CompanyName],[CompanyCode],[Address],[Owner],[Memo])" +
"VALUES('" + datarow["CompanyName"].ToString() + "'" +
",'" + datarow["CompanyCode"].ToString() + "'" +
",'" + datarow["Address"].ToString() + "'" +
",'" + datarow["Owner"].ToString() + "'" +
",'" + datarow["Memo"].ToString() + "')";
using (SqlConnection sqlconn = new SqlConnection(connectionString))
{
sqlconn.Open();

SqlCommand sqlcommand = new SqlCommand(sql, sqlconn);
sqlcommand.ExecuteNonQuery();
sqlconn.Close();
}
}

二:使用sqldataadapter.update(dataset,tablename);

SqlCommand insertcommand = new SqlCommand("INSERT INTO [Table_1]([CompanyName],[CompanyCode],[Address],[Owner],[Memo])" +
"VALUES(@CompanyName, @CompanyCode,@Address,@Owner,@Memo)",new SqlConnection(connectionString));
insertcommand.Parameters.Add("@CompanyName", SqlDbType.NChar, 50, "CompanyName");
insertcommand.Parameters.Add("@CompanyCode", SqlDbType.NChar, 25, "CompanyCode");
insertcommand.Parameters.Add("@Address", SqlDbType.NChar, 255, "Address");
insertcommand.Parameters.Add("@Owner", SqlDbType.NChar, 25, "Owner");
insertcommand.Parameters.Add("@Memo", SqlDbType.NChar, 255, "Memo");
sqldataadapter.InsertCommand = insertcommand;

sqldataadapter.Update(dataset, "Table_1"); 

三:使用sqlbulkcopy.writetoserver(datatable)

SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction);
sqlbulkcopy.DestinationTableName = "Table_1";//数据库中的表名

sqlbulkcopy.WriteToServer(dataset.Tables[0]);