How to pass User Defined Table Type as Stored Proc

2019-01-05 03:57发布

In SQL Server 2008, we can define a table type and use it as Stored Procedures' parameters. But How can I use it in C# invocation of this SP? In other words, how to create a table or list and pass it into Stored Procedure in C# code with this new feature of SQL Server 2008?

3条回答
该账号已被封号
2楼-- · 2019-01-05 04:05

The easiest way is by passing a DataTable as the parameter. Check out some examples here.

查看更多
Deceive 欺骗
3楼-- · 2019-01-05 04:08

From Table-Valued Parameters, linked to in Jeff Meatball Yang's answer:

System.Data.SqlClient supports populating table-valued parameters from DataTable, DbDataReader or System.Collections.Generic.IEnumerable ([T:System.Collections.Generic.IEnumerable`1)] objects. You must specify a type name for the table-valued parameter by using the TypeName property of a SqlParameter. The TypeName must match the name of a compatible type previously created on the server. The following code fragment demonstrates how to configure SqlParameter to insert data.

查看更多
欢心
4楼-- · 2019-01-05 04:10

You need to see this example on CodeProject.

SqlParameter param = cmd.Parameters.AddWithValue("@FileDetails", dt); 

where dt is a DataTable, and the @fileDetails parameter is a table type in SQL:

create type FileDetailsType as table
(
    FileName        varchar(50),
    CreatedDate        varchar(50),
    Size       decimal(18,0)
)

Edit: This MSDN Developer's Guide article also would help.

查看更多
登录 后发表回答