经典 ADO.NET - 如何将 UDT 传递给存储过程?
我有这个 SQL Server 2008 UDT:
CREATE TYPE [dbo].[IdentityType] AS TABLE(
[Id] [int] NOT NULL
)
非常简单。基本上让我可以保留一个 id 列表。
我有这个存储过程:
CREATE PROCEDURE [dbo].[Scoring_ScoreMultipleLocations]
@LocationIds [IdentityType] READONLY,
@DoRanking BIT = 0
AS
BEGIN
-- irrelevant code.
END
Entity Framework 4.0 不支持执行将用户定义的表类型作为参数的存储过程,因此我将恢复到经典的 ADO.NET。唯一的其他选项是传递逗号分隔的字符串。
不管怎样,我找到了这篇文章,但有点难以理解。
我不明白这行代码在做什么:
DataTable dt = preparedatatable();
他们甚至不提供该方法,所以我不知道我应该在这里做什么。
这是我的方法签名:
internal static void ScoreLocations(List<int> locationIds, bool doRanking)
{
// how do i create DataTable??
}
所以我有一个 int
列表,需要将其注入 UDT。
谁能帮我/指出一篇关于如何实现这一目标的清晰文章?
I have this SQL Server 2008 UDT:
CREATE TYPE [dbo].[IdentityType] AS TABLE(
[Id] [int] NOT NULL
)
Pretty simple. Basically allows me to hold onto a list of id's.
I have this stored procedure:
CREATE PROCEDURE [dbo].[Scoring_ScoreMultipleLocations]
@LocationIds [IdentityType] READONLY,
@DoRanking BIT = 0
AS
BEGIN
-- irrelevant code.
END
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.
I don't understand what this line of code is doing:
DataTable dt = preparedatatable();
They don't even provide that method, so i have no idea what i'm supposed to do here.
This is my method signature:
internal static void ScoreLocations(List<int> locationIds, bool doRanking)
{
// how do i create DataTable??
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
本文可能会提供更多帮助。
本质上,您将创建一个与架构匹配的新 DataTable,然后将其作为参数传递。
preparedatatable() 的代码可能类似于:
之后,您必须添加 locationIds:
然后将 dt 指定为参数:
This article might be a bit more help.
Essentially, you'll create a new DataTable that matches the schema, then pass it as a parameter.
The code of preparedatatable() probably would look something like:
After which, you'd have to add your locationIds:
Then assign dt as a parameter: