经典ADO.NET - 如何通过UDT存储过程?存储过程、经典、NET、ADO

2023-09-03 00:34:57 作者:↘因為噯伱︶ㄣ所苡恠苸、

我有这个SQL Server 2008的UDT:

CREATE TYPE [dbo].[IdentityType] AS TABLE(
    [Id] [int] NOT NULL
)

pretty的简单。基本上可以让我守住的ID列表。

Pretty simple. Basically allows me to hold onto a list of id's.

我有这个存储过程:

CREATE PROCEDURE [dbo].[Scoring_ScoreMultipleLocations]
    @LocationIds [IdentityType] READONLY,
    @DoRanking BIT = 0
AS
BEGIN
   -- irrelevant code.
END

实体框架4.0不支持执行该采取用户定义的表类型作为参数的存储过程,所以我恢复到经典的ADO.NET。唯一的另一种选择是通过一个逗号分隔的字符串。

Entity Framework 4.0 does not support executing stored procedures that take user defined table types as a parameter, so i'm reverting to classic ADO.NET. The only other option is passing a comma-seperated string.

不管怎样,我发现这个的文章,但它是一个有点难以遵循。

Anyway, i found this article, but it's a little hard to follow.

我不明白这行code是这样做的:

I don't understand what this line of code is doing:

DataTable的DT = preparedatatable();

DataTable dt = preparedatatable();

他们甚至不提供的方法,所以我不知道我应该在这里做。

They don't even provide that method, so i have no idea what i'm supposed to do here.

这是我的方法签名:

internal static void ScoreLocations(List<int> locationIds, bool doRanking)
{
   // how do i create DataTable??
}

让我有 INT 的列表,需要泵的进UDT。

So i have a list of int and need to pump that into the UDT.

谁能帮我出/点我就如何实现这一目标清晰的文章?

Can anyone help me out/point me to a clear article on how to achieve this?

推荐答案

本文可能有点更多的帮助。

This article might be a bit more help.

从本质上讲,你将创建一个新的DataTable的模式相匹配,然后将其作为一个参数。

Essentially, you'll create a new DataTable that matches the schema, then pass it as a parameter.

的preparedatatable的code()可能会看起来是这样的:

The code of preparedatatable() probably would look something like:

var dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
return dt;

在这,你必须添加locationIds:

After which, you'd have to add your locationIds:

foreach(var id in locationIds)
{
    var row = dt.NewRow();
    row["Id"] = id;
    dt.Rows.Add(row);
}

然后分配DT作为一个参数:

Then assign dt as a parameter:

var param = cmd.Parameters.AddWithValue("@LocationIDs", dt);
param.SqlDbType = SqlDbType.Structured;
param.TypeName = "dbo.IdentityType";