SQL批量插入表类 SqlBulkInsert

ado.net已经有了sqlBulkCopy, 但是那个用xml格式,网络传输数据量太大。

自己实现了一个,传输尽量少的字节。 性能没对比过,有需要的自己拿去测试。

  1 using System.Data.SqlClient;
  2
  3 namespace RaywindStudio.DAL {
  4
  5     /// <summary>
  6     /// MSSQL批量插入表
  7     /// </summary>
  8     public static class SqlBulkInsert {
  9
 10         private static bool initsql = false;
 11
 12         /// <summary>
 13         /// SQL批量插入表 过程
 14         /// </summary>
 15         /// <param name="tableName">表名</param>
 16         /// <param name="fields">字段,逗号分隔</param>
 17         /// <param name="Values">字段值,逗号分隔</param>
 18         /// <param name="RowSplit">Values行间分隔符</param>
 19         /// <param name="RowCount">Values行数</param>
 20         /// <param name="conn">Sql Connection</param>
 21         /// <returns></returns>
 22         public static bool BulkInsert(string tableName, string fields, string Values, string RowSplit, int RowCount,
 23             SqlConnection conn) {
 24             initSql(conn);
 25             string ret= SqlAdo.ExecuteScalar("EXEC [dbo].[pSqlBulkInsert]"
 26                 + " @tableName=N‘" + tableName
 27                 + "‘,@fields=N‘" + fields
 28                 + "‘,@values=N‘" + Values
 29                 + "‘,@split=N‘" + RowSplit
 30                 + "‘,@rowCount=" + RowCount.ToString(), conn).ToString();
 31             return ret == "0";
 32         }
 33         private static void initSql(SqlConnection sqlconn) {
 34             if (!initsql) {
 35                 SqlAdo.ExecuteNonQuery(
 36                 @"IF not EXISTS (SELECT * FROM dbo.SysObjects
 37                             WHERE ID = object_id(N‘[fGetArrayStr]‘)
 38                                 and OBJECTPROPERTY(ID,‘IsScalarFunction‘)=1)
 39                             begin
 40                             exec(
 41                                 ‘CREATE function fGetArrayStr
 42                                 (
 43                                     @str nvarchar(max), --字符串
 44                                     @split nvarchar(10), --分隔符
 45                                     @index int --取第几个元素
 46                                     )
 47                                 returns nvarchar(4000)
 48                                 as
 49                                 begin
 50                                     declare @location int
 51                                     declare @start int
 52                                     declare @next int
 53                                     declare @seed int
 54                                     set @str=ltrim(rtrim(@str))
 55                                     set @start=1
 56                                     set @next=1
 57                                     set @seed=len(@split)
 58                                     set @location=charindex(@split,@str)
 59                                     while @location<>0 and @index>@next
 60                                     begin
 61                                     set @[email protected][email protected]
 62                                     set @location=charindex(@split,@str,@start)
 63                                     set @[email protected]+1
 64                                     end
 65                                     if @location =0 set @location =len(@str)+1
 66                                 return substring(@str,@start,@[email protected])
 67                                 end‘)
 68                             end", sqlconn);
 69
 70                 SqlAdo.ExecuteNonQuery(
 71                     @"IF not EXISTS (SELECT * FROM dbo.SysObjects
 72                         WHERE ID = object_id(N‘[pSqlBulkInsert]‘)
 73                             and OBJECTPROPERTY(ID,‘IsProcedure‘)=1)
 74                             begin
 75                             exec(
 76                                 ‘Create proc pSqlBulkInsert
 77                                 @tableName nvarchar(50),
 78                                 @fields nvarchar(500),
 79                                 @values nvarchar(max),
 80                                 @split nvarchar(5),
 81                                 @rowCount int
 82                                 as
 83                                 declare @next int =1
 84                                 declare @sql nvarchar(500)=N‘ Insert Into ‘[email protected] +N‘(‘[email protected] +N‘) Values(‘
 85                                 declare @sqlTmp nvarchar(4000)
 86                                 Begin tran
 87                                     while @next<[email protected]
 88                                     begin
 89                                     set @[email protected]+dbo.fGetArrayStr(@values,@split,@next)+N‘)‘
 90                                     Exec(@sqlTmp)
 91                                     if(@@error<>0)
 92                                         begin
 93                                             rollback
 94                                             return -1
 95                                         end
 96                                         set @[email protected]+1
 97                                     end
 98                                 commit
 99                                 return 0", sqlconn);
100
101                 initsql = true;
102             }
103         }
104     }
105 }

时间: 2024-12-24 19:03:16

SQL批量插入表类 SqlBulkInsert的相关文章

SQL SERVER 使用BULK Insert将txt文件中的数据批量插入表中(1)

1/首先建立数据表 CREATE TABLE BasicMsg( RecvTime FLOAT NOT NULL , --接收时间,不存在时间相同的数据 AA INT NOT NULL, --24位地址码 . FlightID Varchar(10) NULL, --航班号) 2/ 建立存储过程 USE DF17DataProIF EXISTS (SELECT * FROM SYS.PROCEDURES WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[BulkDataP

SQL语句-批量插入表(表数据插表)

批量插入表(表数据插表) ****1.INSERT INTO SELECT语句语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量.示例如下: 2.SELECT INTO FROM语句语句形式为:SELECT vale1, value2 into Table2 from

批量插入实体类转化DataTable

/// <summary> /// 根据实体类得到表结构 /// </summary> /// <param name="model">实体类</param> /// <returns></returns> private DataTable CreateData<T>(T model) { DataTable dataTable = new DataTable(typeof(T).Name); for

sql - 批量插入数据

直接构造 insert into t (c1, c2, c3) values (1, 1, 1), (2, 2, 2) 使用UNION insert into t (c1, c2, c3) (select a1, a2, a3) union all (select b1, b2, b3) 利用查询结果 insert into t (c1, c2, c3) (select a1, a2, a3 from t2) sql - 批量插入数据,布布扣,bubuko.com

SqlBulkCopy与触发器,批量插入表(存在则更新,不存在则插入)

临时表:Test /****** 对象: Table [dbo].[Test] 脚本日期: 05/10/2013 11:42:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Test]( [ID] [int] IDENTITY(1,1) NOT NULL, [UserID] [int] NOT NULL, [UserName] [nvarchar](50) COLLATE Chines

Delphi中SQL批量插入记录

在进行数据库操作时, 我们经常会遇到批量向数据库中写入记录的情况. 在这里我提供3种操作方式: 1.  单条语句循环插入, 这是最笨的方法:for i := 0 to 100 do  ADOCommand1.Execute('IINSERT INTO .....')   //伪码, 只是说明意思 2.  多条语句批量插入, 将多条Insert语句拼成一条语句, 一次执行:for i := 0 to 100 do  sqlstr := sqlstr + 'INSERT INTO ...';ADOC

SQL批量插入数据

MS SQL Server数据库批量插入数据的sql语句总结: 第一种: insert into tb_NameList(Name,InsertDate,UpdateDate) values('aa',GETDATE(),GETDATE()) insert into tb_NameList(Name,InsertDate,UpdateDate) values('bb',GETDATE(),GETDATE()) insert into tb_NameList(Name,InsertDate,Upd

Sql批量插入方法

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.Common;using System.Data.Entity;using System.Data.SqlClient;using System.Linq; namespace TY.Tools{ /// <summary> /// 批量插入 /// </summary&

SQL批量插入、修改

1. 批量插入 insert into table_A  ( ORDER_CODE, PAYMENT_CODE) values <foreach item="item" index="index" collection="list" separator=","> (#{item.ORDER_CODE},if(#{item.PAYMENT_CODE}='',NULL,#{item.PAYMENT_CODE})) 2.