通过Linq调用存储过程

编程入门 行业动态 更新时间:2024-10-08 02:29:19
本文介绍了通过Linq调用存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个表Client,字段ID为int,名称为varchar,地址为varchar &已创建如下存储过程:

Hi, I have a table Client having fields Id int,name varchar,address varchar & have created stored procedure as below:

create procedure ClientInsert @id int, @name varchar(50), @address varchar(100) as insert into Client (ID,Name,Address)values(@id,@name,@address).

问题是我想使用存储过程在表中使用Linq插入值. 请让我知道如何实现.

The thing is that I want to insert the values using Linq in the table using stored procedure. Please let me know how to implement.

推荐答案

最后的评论... 您需要从文本框中捕获值,然后通过数据上下文将其传递给存储的proc.类似于以下内容: Per your last comment... You would need to capture the value from the textbox and pass it through to the stored proc via the data context. Something similar to this: string clientName = txtClientName.Text; string clientAddress = txtClientAddress.Text; YourDataContext dc = new YourDataContext(); dc.ClientInsert(yourId, clientName, clientAddress);

同样,我不会生成ID客户端,而是从存储的proc返回它,所以我更愿意看到以下内容:

Again, I would not generate an ID client side, I would return it from the stored proc, so I would prefer to see this:

string clientName = txtClientName.Text; string clientAddress = txtClientAddress.Text; int? clientID = 0; YourDataContext dc = new YourDataContext(); dc.ClientInsert(ref clientID, clientName, clientAddress);

编辑以修复第二种方法... 干杯.

Edited to fix second approach... Cheers.

您可以检查此线程. Linq to Sql Server中的存储过程 [ ^ ] You can check this thread. Stored Procedure in Linq to Sql Server[^]

如果您使用的是LINQ to SQL,则可以将存储的proc从Server Explorer工具窗口拖到DBML图上. 然后,可以通过您的数据上下文使用存储的proc. 您可能还希望返回SCOPE_IDENTITY,以便调用代码能够知道您刚刚插入的记录的生成ID. 干杯. If you''re using LINQ to SQL, you can drag the stored proc from the Server Explorer tool window onto your DBML diagram. The stored proc will then be available through your Data Context. You would likely want to return SCOPE_IDENTITY as well, so that the calling code would be able to know what the generated ID was of the record you''ve just inserted. Cheers.

更多推荐

通过Linq调用存储过程

本文发布于:2023-10-19 01:53:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1506039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:存储过程   Linq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!