是否可以在 LINQPad 中使用 LINQ 调用存储过程?

发布于 2024-10-01 19:59:14 字数 267 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

想你只要分分秒秒 2024-10-08 19:59:14

至少在我的 LINQPad 副本中,存储过程显示在数据库树视图中并且可以直接调用。

这是屏幕截图:

Screenshot

At least in my copy of LINQPad, stored procedures show up in the database treeview and can be invoked directly.

Here's a screenshot:

Screenshot

生生不灭 2024-10-08 19:59:14

总结一些其他答案并添加一些附加信息:

使用默认 (LINQ to SQL) 驱动程序连接到数据源。确保选中包括存储过程和函数复选框。

存储过程和函数现在可在使用连接的查询中作为 .NET 函数(例如 C#)使用。函数所需的参数反映了存储过程或数据库函数所需的参数。

该函数返回的值是一个 ReturnDataSet,它是从 DataSet 派生的 LINQPad 类型。不幸的是,对数据集执行 LINQ 查询并不那么容易,但 LINQPad 提供了扩展方法 AsDynamic() ,它将获取返回数据集的第一个表(通常只有一个表)并转换行到 IEnumerable,其中集合中的对象是动态的,允许您将列值作为属性访问。例如,如果您的存储过程返回列 IdName,您可以使用 LINQ:

SomeStoredProc().AsDynamic().Where(row => row.Id == 123 && row.Name == "Foo")

不幸的是,您不会有智能感知,因为行对象是动态的。

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 from DataSet. Unfortunately it is not so easy to perform LINQ queries on data sets but LINQPad provides the extension method AsDynamic() that will take the first table of the returned data set (normally there is only one table) and convert the rows to IEnumerable<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 columns Id and Name you can use LINQ:

SomeStoredProc().AsDynamic().Where(row => row.Id == 123 && row.Name == "Foo")

Unfortunately you will not have intellisense because the row objects are dynamic.

蒗幽 2024-10-08 19:59:14

我的回答(在丹尼尔的帮助下,谢谢。)

丹尼尔帮助我认识到,如果您使用查询窗口中的下拉列表定位数据库,则可以调用存储过程;然后在查询窗口中通过名称调用存储过程,并将括号添加到末尾以将其作为函数调用。

当前版本(截至目前我使用的是 4.26.2)之间的主要区别是 VS 中的 LINQ 返回自定义数据类型以匹配从存储过程返回的数据对象,而 LINQPad 返回 DataSet。因此,通过选择“C# 语句”,您可以成功地将其作为查询运行:

DataSet fooResults = foo_stored_proc(myParam);

感谢大家的帮助!

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:

DataSet fooResults = foo_stored_proc(myParam);

Thanks for everyone's help!

故人的歌 2024-10-08 19:59:14

示例:假设我有一个名为 PersonMatchNoDOBRequired 的存储过程,如果我想要进一步过滤,它希望我传递名字和姓氏

var b = PersonMatchNoDOBRequired("John", "Smith").AsDynamic();
b.Dump();

(在我的例子中,我在 sql 中有一个高级算法,可以对第一个和第一个进行评分姓氏,名字可以返回“Jodi”,但是说我真的想进一步过滤它,那么它是这样的:

var b = PersonMatchNoDOBRequired("John", "Smith").AsDynamic().Where(x => x.FirstName == "John" && x.LastName == "Smith");   

其他答案部分正确 - 我没有看到参数中传递任何内容(如果需要)。

Example: Say I have a Stored Procedure called PersonMatchNoDOBRequired and it expects me to pass in a First and Last Name

var b = PersonMatchNoDOBRequired("John", "Smith").AsDynamic();
b.Dump();

If 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:

var b = PersonMatchNoDOBRequired("John", "Smith").AsDynamic().Where(x => x.FirstName == "John" && x.LastName == "Smith");   

Other answers are partially right - I didn't see anything passing in the parameters ( if needed).

注定孤独终老 2024-10-08 19:59:14

使用 @jlafay 使用的示例,您可以从表中获取行并按名称引用列。

DataSet spItem = sp_StoredProcedure(parameters);
var rows = spItem.Tables[0].Rows;
foreach (DataRow row in rows)
{
   // Do something
   Console.WriteLine($"{row["MyColumn"]}, {row["LastName"]}");
} 

Using the example that @jlafay used you can grab the rows from the table and reference the column by name.

DataSet spItem = sp_StoredProcedure(parameters);
var rows = spItem.Tables[0].Rows;
foreach (DataRow row in rows)
{
   // Do something
   Console.WriteLine($"{row["MyColumn"]}, {row["LastName"]}");
} 
谈场末日恋爱 2024-10-08 19:59:14

我使用的是 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

謸气贵蔟 2024-10-08 19:59:14

您可以仅保存使用 C# 和标准 ADO.NET 对象(SqlConnectionSqlCommand 等)的查询,并 Dump() 结果。

我意识到它没有使用 LINQ,但它对我很有帮助。

You could just save a query that uses C# with standard ADO.NET objects (SqlConnection, SqlCommand, etc) and Dump() the results.

I realize it's not using LINQ, but it has served me well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文