通过Linq调用存储过程 [英] Call Stored Procedure through Linq

查看:60
本文介绍了通过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调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆