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

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - Michael.li

excel中使用sum函数计算包含小数列,结果出现多位小数值 使用Wireshark抓包QQ聊天用户IP地址 MSSQL数据库设计心得 Html5大文件断点续传 c# 使用protobuf格式操作 Redis Web网站架构设计 extjs中的tabpanle下的combobox提交问题 Asp.net下from认证统一认证配置 Extjs 4.07 对类型定义引发的匹配问题 Supesoft权限管理系统(FrameWork) 1.0.9 Release 手机6120C 玩仙剑dos版 ASP.NET权限管理系统(FrameWork) 1.0.8 Release ASP.NET权限管理系统(FrameWork) 1.0.7 Release 关于在Page_Load定义外部变量输出 - Michael.li - 博客园 广告统计分析系统(ADCount) 1.0.0 Beta DDBuildTools 1.3.0 Release发布 ASP.NET权限管理系统(FrameWork) 1.0.6 Release remoting缓存设计总结 - Michael.li - 博客园 ASP.NET权限管理系统(FrameWork) 1.0.5 Release
使用 SqlBulkCopy 向 destinationTableName 表插入数据
Michael.li · 2023-03-28 · via 博客园 - Michael.li
public static class SqlConnectionExtension
    {
        /// <summary>
        /// 使用 SqlBulkCopy 向 destinationTableName 表插入数据
        /// </summary>
        /// <typeparam name="TModel">必须拥有与目标表所有字段对应属性</typeparam>
        /// <param name="conn">连接字符串</param>
        /// <param name="modelList">要插入的数据</param>
        /// <param name="batchSize">SqlBulkCopy.BatchSize</param>
        /// <param name="destinationTableName">表名</param>        
        public static int BulkCopy<TModel>(string connstr, List<TModel> list, int batchSize, string destinationTableName)
        {
            var table = new DataTable();

            var type = typeof(TModel);
            int rValue = 0;
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                SqlCommand cmd = new SqlCommand("select * from [" + destinationTableName + "] where 1=2", conn);

                // 执行
                conn.Open();
                var reader = cmd.ExecuteReader();
                table.Load(reader);

                foreach (var item in list)
                {
                    var row = table.NewRow();
                    foreach (DataColumn column in table.Columns)
                    {
                        row[column] = type.GetProperty(column.ColumnName).GetValue(item, null) ?? DBNull.Value;
                    }
                    table.Rows.Add(row);
                }
                reader.Close();

                SqlBulkCopy bulkCopy = new SqlBulkCopy(conn as SqlConnection);
                bulkCopy.DestinationTableName = destinationTableName;
                bulkCopy.BatchSize = batchSize;

                try
                {
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    var dataReader = table.CreateDataReader();

                    if (table != null && table.Rows.Count != 0)
                        bulkCopy.WriteToServer(dataReader);
                    rValue = table.Rows.Count;
                }
                catch (Exception ex)
                {
                    rValue = 0;
                    conn.Close();
                    throw ex;
                }
                finally
                {
                    if (bulkCopy != null)
                        bulkCopy.Close();
                }                
            }

            return rValue;
        }
    }