Home Database Mysql Tutorial mssql2008 自定义表类型实现(批量插入或者修改)_MySQL

mssql2008 自定义表类型实现(批量插入或者修改)_MySQL

Jun 01, 2016 pm 01:23 PM
database website Record

bitsCN.com 在做大型网站或者系统的时候,经常会遇到个问题就是批量插入或者修改数据库。如果遇到这个问题,还是一条条记录来处理,这样的效率太低了,所以要考虑批量插入或者修改

今天这边不讲SqlBulkCopy,只简单讲sql自定义表类型。因为目前的项目我用到了分表的方式,用存储过程比较方便自动创建分表。现在把我目前写的功能简单做个记录,也方便以后自己查阅

第一步,在数据库里点击 新建查询 创建相应的sql

--======自定义表类型Demo======
--创建表
create table tab
(
tabvalue varchar(10)
)
go
--创建自定义表类型
create type Type_tab as table
(
Type_tabvalue varchar(10)
)
go
--创建 存储过程
create procedure p_Tab
(
@ptab Type_tab readonly
)
as
begin
insert into tab select * from @ptab
end
--sql 测试表类型 type1
declare @tt Type_tab
insert into @tt values('a')
insert into @tt values('b')
insert into @tt values('c')
insert into tab select * from @tt
select * from tab
--sql 测试表类型 type2
declare @tt2 Type_tab
insert into @tt2 values('a')
insert into @tt2 values('b')
insert into @tt2 values('c')
execute p_Tab @tt2

第二步 asp.net 调用 存储过程

using (SqlConnection conn =SqlHelper.Settings.GetSqlConnection(true))
{
try
{
DataTable DtAdd = new DataTable();
DtAdd.Columns.Add("tabvalue");
DtAdd.Columns["tabvalue"].DataType = System.Type.GetType("System.String");
foreach (ModelEnt rl in List)
{
DataRow Dr = DtAdd.NewRow();
Dr["tabvalue"] = rl.tabvalue;
DtAdd.Rows.Add(Dr);
}
//int Rt = SqlHelper.SqlHelper.SqlBulkCopy(DtAdd, TableName, conn);
SqlCommand cmd = new SqlCommand("p_Tab", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ptab", SqlDbType.Structured).Value =DtAdd;
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
#if DEBUG
throw ex;
#else
return -10000;
#endif
}
finally
{
conn.Close();
}

ModelEnt 是tab表对应的实体类。list 是ModelEnt 的一个集合,这样可以实现多个row,这样就能一次插入多条记录。bitsCN.com

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

How does Go language implement the addition, deletion, modification and query operations of the database?

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

Detailed tutorial on establishing a database connection using MySQLi in PHP

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

How does Hibernate implement polymorphic mapping?

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

An in-depth analysis of how HTML reads the database

Analysis of the basic principles of MySQL database management system Analysis of the basic principles of MySQL database management system Mar 25, 2024 pm 12:42 PM

Analysis of the basic principles of MySQL database management system

Tips and practices for handling Chinese garbled characters in databases with PHP Tips and practices for handling Chinese garbled characters in databases with PHP Mar 27, 2024 pm 05:21 PM

Tips and practices for handling Chinese garbled characters in databases with PHP

How does Go WebSocket integrate with databases? How does Go WebSocket integrate with databases? Jun 05, 2024 pm 03:18 PM

How does Go WebSocket integrate with databases?

See all articles