创建存储过程(可自行扩展ID不存在时为新增,否为修改)
create PROCEDURE [dbo].[p_Company_Insert]
@CompanyCollection [CompanyTableType] READONLY
AS
INSERT INTO tb_Company (
ID
,Name
,type
)
SELECT
a.ID
,a.Name
,a.type
FROM @CompanyCollection AS a;
GO
创建数据库
CREATE TYPE [dbo].[tb_Company] AS TABLE(
[ID] [int] NOT NULL,
[Name] nvarchar NULL,
[type] nvarchar NULL,
)
GO
执行代码
///
/// 把datatable当参数,批量添加数据库中,返回datatable的新增行
///
///
///
public DataTable Company_Insert(DataTable tb)
{
DataTable dt = null;
CMD.CommandText = “p_BuySell_Insert”;
CMD.CommandType = CommandType.StoredProcedure;
CMD.Parameters.Clear();
CMD.Parameters.AddWithValue(“@CompanyCollection”, tb);
dt = DB.DataTable(CMD);
return dt;
}