从 LINQ To SQL ExecuteQuery 中的存储过程捕获输出参数
执行存储过程时是否可以从 LINQ To SQL DataContext 获取输出参数?
IEnumerable<Address> result =
ExecuteQuery<Address>(((MethodInfo)(MethodInfo.GetCurrentMethod())),
address, pageIndex, pageSize, totalCount);
其中,address
、pageIndex
和 pageSize
是输入参数,TotalCount
是输出参数。
如何捕获输出参数?
这是另一种尝试,但再次无法获取参数值:
[Function(Name = "Telecom.AddressSearch")]
private IEnumerable SearchAddress([Parameter(Name = "address", DbType = "varchar")] string address,
[Parameter(Name = "pageIndex", DbType = "int")] int pageIndex,
[Parameter(Name = "pageSize", DbType = "int")] int pageSize,
[Parameter(Name = "totalCount", DbType = "int")] ref int totalCount)
{
IEnumerable result = ExecuteQuery(((MethodInfo)(MethodInfo.GetCurrentMethod())), address, pageIndex, pageSize, totalCount);
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
斯科特·格思里(Scott Guthrie)有一个 博客文章,介绍如何让 LINQ to SQL 与存储过程配合使用。虽然他的帖子没有直接回答您的问题,但它提供了一些可能有效的线索。在 .dbml 类中定义方法时,“LINQ to SQL 将 SPROC 中的‘out’参数映射为引用参数(ref 关键字)。”您可以尝试使用 ref 关键字并查看 TotalCount 是否已更新。
更新:
我做了一些更多的研究,并找到了一种适合您的方法。这是来自 MSDN 文章。您需要扩展您的 DataContext,但这不应该是太大的问题(看来您可能已经在这样做了)。查看该文章以获取更多信息,但基本内容是:
其中“this”是您的 DataContext。
更新 2:
IExecuteResult 有一个 ReturnValue 属性。它是 Object 类型,因此您必须对其进行转换才能获得结果,但它应该允许您获得结果的 Enumerable。在你的情况下,这样的事情应该有效:
Scott Guthrie has a blog post that describes how to get LINQ to SQL to work with stored procedures. While his post does not directly answer your question, it provides a clue to something that may work. When defining a method in a .dbml class, "LINQ to SQL maps 'out' parameters in SPROCs as reference parameters (ref keyword)." You might try and use the ref keyword and see if the totalCount gets updated.
Update:
I did some more research, and have found a way that should work for you. This is from an MSDN article. You would need to extend your DataContext, but that shouldn't be too big of an issue (it appears you may already be doing this). Check out the article for more information, but the basic piece is this:
Where 'this' is your DataContext.
Update 2:
The IExecuteResult has a ReturnValue property. It is of type Object, so you will have to cast it to get the results, but it should allow you to get the Enumerable of the results. In your case, something like this should work: