如何在wcf服务中使用存储过程并使用linq返回json到sql [英] How to use stored procedure in wcf service and return in json with linq to sql

查看:110
本文介绍了如何在wcf服务中使用存储过程并使用linq返回json到sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想如何使用linq到sql类的json格式的wcf中的存储过程来检索数据




我想使用JSON格式的WCF检索存储过程的输出,我使用的是LINQ to SQL类。

Hi ,

I want to how to retrive data using store procedure in wcf in json format with linq to sql classes


I want to retrieve store procedure's out put using WCF in JSON format and I am using LINQ to SQL classes.

推荐答案







在LINQ中使用存储过程的六个简单步骤 [ ^ ]



当您将过程添加到DBML时,它会自动创建输出类,您可以将其用作数据合同并在JSON中返回相同的数据
Hi,


Six simple steps to use a Stored Procedure in LINQ[^]

When you add Procedure to DBML then its automatically create output class and which you can use as a Data Contract and returned the same data in JSON


您的要求是使用Linq To Sql意味着一个DBML文件。

并且你有一个商店程序。

现在正在执行storeprocedure并希望从商店程序输出





这里我正在创建一个商店p

your requirement is you are using Linq To Sql means a DBML file.
and you are having a store procedure on it.
now are executing the storeprocedure and want the output from store procedure


here i am creating a store p
CREATE PROCEDURE Stock.usp_CheckBuyBookByISBN  
 -- Add the parameters for the stored procedure here  
 (  
 @vcISBN varchar(50),  
 @retvalue int=0 output  
 )  
 AS  
 BEGIN  
 SET NOCOUNT ON;  
  DECLARE @return  int   
  SET @return  = (SELECT COUNT(intBUyBAckid) from Stock.BuybackBooks WHERE vcISBN13=@vcISBN AND decPrice>0 )  
  SET @retvalue=@return  
 End  







这里我调用名为usp_CheckBuyBookByISBN的storeprocedure,它将返回一个整数值。< br $>







here i call the storeprocedure with name usp_CheckBuyBookByISBN and it will return a integer a value.


DataClassesDataContext db = new DataClassesDataContext();
int? b = null;
int a = db.usp_CheckBuyBookByISBN("9780199795352", ref b);







这是我们如何使用Linq To Sql从存储过程中获取输出

..





现在你有一个json格式的wcf服务。

for achving this在wcf中创建一个函数并用一个名为WEBGET的属性装饰它

就像这样。






this is how we fetch a the output from store procedure using Linq To Sql
..


now you have a wcf service with json format.
for achving this create a function in wcf and decorate it with a Attribute called as WEBGET
like this.

[OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public int GetResult()
    {
DataClassesDataContext db = new DataClassesDataContext();
        int? b = null;
        int a = db.usp_CheckBuyBookByISBN("9780199795352", ref b);
return a;
}







现在只需从您的网站调用该功能..

希望这个帮助.....




now simply call that function from you website..
Hope this help.....


这篇关于如何在wcf服务中使用存储过程并使用linq返回json到sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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