2023年6月21日发(作者:)

C#快速插⼊⼤量数据到数据库中有时需要通过C#⼀次性插⼊或更新⼤量数据到SQL Server中,使⽤insert into/update这种⽅式就会变得异常缓慢,这个时候可以使⽤到表值参数来⼀次性插⼊或更新⼤量数据。需要注意,UpdateSale⽅法table参数的结构需要和表⾃定义类型结构、数据库表结构⼀样,⽐如下⾯的Type_SaleUpdate表⾃定义类型有id、Name、CreateDate三个字段,那么传进来的table参数也要有这三个字段,总之table参数的结构要和表⾃定义类型、数据库表⼀样。下⾯是具体代码:

1 public static void UpdateSale(DataTable table) 2 { 3   if ( <= 0) 4   return; 5   List sqlList = new List() 6   { 7     "if exists(select * from s where name = 'DownLoad_SaleInsertProc') drop proc DownLoad_SaleInsertProc", //判断存储过程是否存在,存在即删除 8     "if exists(select * from where name = 'Type_SaleInsert') drop type Type_SaleInsert", //判断表⾃定义类型是否存在,存在即删除 9     "if exists(select * from s where name = 'DownLoad_SaleUpdateProc') drop proc DownLoad_SaleUpdateProc",10     "if exists(select * from where name = 'Type_SaleUpdate') drop type Type_SaleUpdate",11

12     @"create type Type_SaleUpdate as table( //创建表⾃定义类型13     Id uniqueidentifier not null,14     Name nvarchar(20) not null,15     CreateDate datetime )",16

17     @"create proc DownLoad_SaleUpdateProc(@Table Type_SaleUpdate readonly) //创建存储过程,使⽤表值参数更新表数据18     as19     begin20     update Sale set21     id=,Name =,CreateDate =Date from Sale a22     left join @Tableb on = where is not null23     end;",24

25     @"create type Type_SaleInsert as table( //创建表⾃定义类型26     Id uniqueidentifier not null,27     Name nvarchar(20) not null,28     CreateDate datetime )",29

30     @"create proc DownLoad_SaleInsertProc(@Table Type_SaleInsert readonly) //创建存储过程,使⽤表值参数插⼊数据31     as32     begin33     insert into Sale select distinct a.* from @Table a34     where (select count(1) from Sale b where = ) = 035     end;"36   };37   SqlParameter[] sqlsUpdate = new SqlParameter[] {new SqlParameter("@Table", ured){ TypeName = "Type_SaleUpdate",Value = table}}; //设置表值参数的值,以及表值参数的数据类型38   SqlParameter[] sqlsInsert = new SqlParameter[] {new SqlParameter("@Table", ured){ TypeName = "Type_SaleInsert",Value = table}};39   eNonQueryProc("", sqlList); //执⾏建表,创建存储过程等SQL语句40   eNonQueryProc("", "DownLoad_SaleUpdateProc", sqlsUpdate); //使⽤存储过程更新数据41   eNonQueryProc("", "DownLoad_SaleInsertProc", sqlsInsert); //使⽤存储过程插⼊数据42 }

2023年6月21日发(作者:)

C#快速插⼊⼤量数据到数据库中有时需要通过C#⼀次性插⼊或更新⼤量数据到SQL Server中,使⽤insert into/update这种⽅式就会变得异常缓慢,这个时候可以使⽤到表值参数来⼀次性插⼊或更新⼤量数据。需要注意,UpdateSale⽅法table参数的结构需要和表⾃定义类型结构、数据库表结构⼀样,⽐如下⾯的Type_SaleUpdate表⾃定义类型有id、Name、CreateDate三个字段,那么传进来的table参数也要有这三个字段,总之table参数的结构要和表⾃定义类型、数据库表⼀样。下⾯是具体代码:

1 public static void UpdateSale(DataTable table) 2 { 3   if ( <= 0) 4   return; 5   List sqlList = new List() 6   { 7     "if exists(select * from s where name = 'DownLoad_SaleInsertProc') drop proc DownLoad_SaleInsertProc", //判断存储过程是否存在,存在即删除 8     "if exists(select * from where name = 'Type_SaleInsert') drop type Type_SaleInsert", //判断表⾃定义类型是否存在,存在即删除 9     "if exists(select * from s where name = 'DownLoad_SaleUpdateProc') drop proc DownLoad_SaleUpdateProc",10     "if exists(select * from where name = 'Type_SaleUpdate') drop type Type_SaleUpdate",11

12     @"create type Type_SaleUpdate as table( //创建表⾃定义类型13     Id uniqueidentifier not null,14     Name nvarchar(20) not null,15     CreateDate datetime )",16

17     @"create proc DownLoad_SaleUpdateProc(@Table Type_SaleUpdate readonly) //创建存储过程,使⽤表值参数更新表数据18     as19     begin20     update Sale set21     id=,Name =,CreateDate =Date from Sale a22     left join @Tableb on = where is not null23     end;",24

25     @"create type Type_SaleInsert as table( //创建表⾃定义类型26     Id uniqueidentifier not null,27     Name nvarchar(20) not null,28     CreateDate datetime )",29

30     @"create proc DownLoad_SaleInsertProc(@Table Type_SaleInsert readonly) //创建存储过程,使⽤表值参数插⼊数据31     as32     begin33     insert into Sale select distinct a.* from @Table a34     where (select count(1) from Sale b where = ) = 035     end;"36   };37   SqlParameter[] sqlsUpdate = new SqlParameter[] {new SqlParameter("@Table", ured){ TypeName = "Type_SaleUpdate",Value = table}}; //设置表值参数的值,以及表值参数的数据类型38   SqlParameter[] sqlsInsert = new SqlParameter[] {new SqlParameter("@Table", ured){ TypeName = "Type_SaleInsert",Value = table}};39   eNonQueryProc("", sqlList); //执⾏建表,创建存储过程等SQL语句40   eNonQueryProc("", "DownLoad_SaleUpdateProc", sqlsUpdate); //使⽤存储过程更新数据41   eNonQueryProc("", "DownLoad_SaleInsertProc", sqlsInsert); //使⽤存储过程插⼊数据42 }