更新时间:2023-01-12 10:54:00
在你的数据库中创建一个User-Defined TableType
:
Create a User-Defined TableType
in your database:
CREATE TYPE [dbo].[MyTableType] AS TABLE(
[Id] int NOT NULL,
[Name] [nvarchar](128) NULL
)
并在您的存储过程
中定义一个参数:
and define a parameter in your Stored Procedure
:
CREATE PROCEDURE [dbo].[InsertTable]
@myTableType MyTableType readonly
AS
BEGIN
insert into [dbo].Records select * from @myTableType
END
并将您的 DataTable
直接发送到 sql server:
and send your DataTable
directly to sql server:
using (var command = new SqlCommand("InsertTable") {CommandType = CommandType.StoredProcedure})
{
var dt = new DataTable(); //create your own data table
command.Parameters.Add(new SqlParameter("@myTableType", dt));
SqlHelper.Exec(command);
}
要编辑存储过程中的值,可以声明一个相同类型的局部变量并将输入表插入其中:
To edit the values inside stored-procedure, you can declare a local variable with the same type and insert input table into it:
DECLARE @modifiableTableType MyTableType
INSERT INTO @modifiableTableType SELECT * FROM @myTableType
然后,您可以编辑@modifiableTableType
:
UPDATE @modifiableTableType SET [Name] = 'new value'