是否可以在 LINQPad 中使用 LINQ 调用存储过程?
在 Visual Studio 中,你有一个很好的设计器,它用一个漂亮的小方法封装了一个存储过程。我非常喜欢 LINQPad,并且每天在工作中使用它(自从我使用它以来,我的工作就不需要打开 SQL Studio!)并且希望在使用它时调用存储过程。
恐怕我知道自己问题的答案,但我希望也许我缺少一个功能,或者也许有人有一些黑魔法可以借给我来实现这一点。顺便说一句,如果这有什么区别的话,我正在使用 LINQPad 4。
编辑
请参阅下面我的回答。
In visual studio you have the nice designer that encapsulates a stored proc with a nifty little method. I totally love LINQPad and use it on a daily basis at work (haven't had a need to open up SQL Studio for my job since I've been using it!) and would like to call stored procs while using it.
I'm afraid I know the answer to my own question but I'm hoping that maybe there's a feature I'm missing or perhaps someone has some black magic they can lend me to make this happen. Btw, I'm using LINQPad 4 if that makes a difference.
Edit
See my answer below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
至少在我的 LINQPad 副本中,存储过程显示在数据库树视图中并且可以直接调用。
这是屏幕截图:
At least in my copy of LINQPad, stored procedures show up in the database treeview and can be invoked directly.
Here's a screenshot:
总结一些其他答案并添加一些附加信息:
使用默认 (LINQ to SQL) 驱动程序连接到数据源。确保选中包括存储过程和函数复选框。
存储过程和函数现在可在使用连接的查询中作为 .NET 函数(例如 C#)使用。函数所需的参数反映了存储过程或数据库函数所需的参数。
该函数返回的值是一个
ReturnDataSet
,它是从DataSet
派生的 LINQPad 类型。不幸的是,对数据集执行 LINQ 查询并不那么容易,但 LINQPad 提供了扩展方法AsDynamic()
,它将获取返回数据集的第一个表(通常只有一个表)并转换行到IEnumerable
不幸的是,您不会有智能感知,因为行对象是动态的。
Summing up some of the other answers as well as adding a bit of additional information:
Connect to your data source using the Default (LINQ to SQL) driver. Make sure that the check box Include Stored Procedures and Functions is checked.
Stored procedures and functions are now available as .NET functions (e.g. C#) in queries using the connection. The parameters required by the function reflects the parameters required by the stored procedure or database function.
The value returned by the function is a
ReturnDataSet
which is a LINQPad type deriving fromDataSet
. Unfortunately it is not so easy to perform LINQ queries on data sets but LINQPad provides the extension methodAsDynamic()
that will take the first table of the returned data set (normally there is only one table) and convert the rows toIEnumerable<Object>
where the objects in the collection are dynamic allowing you to access the column values as properties. E.g. if your stored procedure returns columnsId
andName
you can use LINQ:Unfortunately you will not have intellisense because the row objects are dynamic.
我的回答(在丹尼尔的帮助下,谢谢。)
丹尼尔帮助我认识到,如果您使用查询窗口中的下拉列表定位数据库,则可以调用存储过程;然后在查询窗口中通过名称调用存储过程,并将括号添加到末尾以将其作为函数调用。
当前版本(截至目前我使用的是 4.26.2)之间的主要区别是 VS 中的 LINQ 返回自定义数据类型以匹配从存储过程返回的数据对象,而 LINQPad 返回 DataSet。因此,通过选择“C# 语句”,您可以成功地将其作为查询运行:
感谢大家的帮助!
My Answer (With Daniel's assistance, thanks.)
Daniel helped me realize that stored procedures can be called if you target a database with the drop down list in the query window; then in the query window call the stored proc by its name and append parentheses to the end to call it as a function.
The main difference between the current version (I'm using 4.26.2 as of this date) is that LINQ in VS returns custom data types to match data objects returned from the stored procedure and LINQPad returns a DataSet. So by selecting "C# Statement(s)" you can successfully run this as a query:
Thanks for everyone's help!
示例:假设我有一个名为
PersonMatchNoDOBRequired
的存储过程,如果我想要进一步过滤,它希望我传递名字和姓氏(在我的例子中,我在 sql 中有一个高级算法,可以对第一个和第一个进行评分姓氏,名字可以返回“Jodi”,但是说我真的想进一步过滤它,那么它是这样的:
其他答案部分正确 - 我没有看到参数中传递任何内容(如果需要)。
Example: Say I have a Stored Procedure called
PersonMatchNoDOBRequired
and it expects me to pass in a First and Last NameIf I want further filtering ( in my case I have an advanced algorithm in sql that scores the first and last name , first name can return "Jodi" , but say I really want to filter it further then it is like this:
Other answers are partially right - I didn't see anything passing in the parameters ( if needed).
使用 @jlafay 使用的示例,您可以从表中获取行并按名称引用列。
Using the example that @jlafay used you can grab the rows from the table and reference the column by name.
我使用的是 4.51.03 版本并连接到 SQL Server 2012 Express 版本。连接到 AdventureWorks2012 数据库后,我可以看到所有存储过程。右键单击存储过程并选择StoredProceedureName(...) 下拉列表,您可以在查询窗口中显示该存储过程。您需要将参数插入包含省略号的括号内并运行查询。
以下两篇文章中显示了示例:
http: //hodentekmsss.blogspot.com/2015/01/learn-querying-sql-server-2012-using.html
http://hodentekmsss.blogspot.com/2015/01/learn-querying-sql-server-2012-using_25.html
I am using 4.51.03 version and connecting to SQL Server 2012 Express edition. After connecting to AdventureWorks2012 database I can see all the stored procedures. Right clicking a stored procedure and choosing StoredProceedureName(...) drop-down, you display the stored procedure in the query window. You need to insert the parameters inside the parenthesis containg the ellipsis and run the query.
Examples are shown in the following two posts:
http://hodentekmsss.blogspot.com/2015/01/learn-querying-sql-server-2012-using.html
http://hodentekmsss.blogspot.com/2015/01/learn-querying-sql-server-2012-using_25.html
您可以仅保存使用 C# 和标准 ADO.NET 对象(
SqlConnection
、SqlCommand
等)的查询,并Dump()
结果。我意识到它没有使用 LINQ,但它对我很有帮助。
You could just save a query that uses C# with standard ADO.NET objects (
SqlConnection
,SqlCommand
, etc) andDump()
the results.I realize it's not using LINQ, but it has served me well.