在LINQ中调用存储过程 [英] Call store procedure in LINQ

查看:129
本文介绍了在LINQ中调用存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在LINQ中调用storerprocedure但这显示错误



我尝试过:



[WebMethod]

public static string search_data(DateTime fromdate,DateTime todate,string region)

{

试试

{

string result =;



Ts1 ts = new Ts1();

列表< ts1> dq = ts.griddataresult(fromdate,todate,region).ToList();



DataTable dt = new DataTable();



dt.Columns.Add(ID,typeof(int));

dt.Columns.Add(Owner,typeof(string));





foreach(var c in dq)

{

dt。 Rows.Add(c.ID,c.owner);

}



result = DataSetToJSON(dt);

返回结果;

}

catch(例外)

{

抛出新的异常();

}

}



公共静态字符串DataSetToJSON(DataTable dt)

{

Dictionary< string,> dict = new Dictionary< string,>();

object [] arr = new object [dt.Rows.Count + 1];



for(int i = 0; i< = dt.Rows.Count - 1; i ++)

{

arr [i] = dt.Rows [i] .ItemArray;

}



dict.Add(response,arr);



JavaScriptSerializer json = new JavaScriptSerializer();

返回json.Serialize(dict);

}

解决方案

这是你应该避免使用var的原因之一,直到你对.net更有经验。 EF认为你的SP就是所谓的标量过程,即它只返回一个值,而不是结果set \table。因此返回值为int,因此您的dq变量为int,并且您只能循环使用集合的变量。看看SP是如何在EF中创建的,可以从EF模型中删除它并重新添加它。


ScottGu&#博客 - LINQ to SQL(第6部分 - 使用存储过程检索数据) [ ^ ]

I try to call storerprocedure in LINQ but this show error

What I have tried:

[WebMethod]
public static string search_data(DateTime fromdate, DateTime todate, string region)
{
try
{
string result = "";

Ts1 ts = new Ts1();
List<ts1> dq = ts.griddataresult(fromdate, todate, region).ToList();

DataTable dt = new DataTable();

dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Owner", typeof(string));


foreach (var c in dq)
{
dt.Rows.Add(c.ID, c.owner);
}

result = DataSetToJSON(dt);
return result;
}
catch (Exception)
{
throw new Exception();
}
}

public static string DataSetToJSON(DataTable dt)
{
Dictionary<string,> dict = new Dictionary<string,>();
object[] arr = new object[dt.Rows.Count + 1];

for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
arr[i] = dt.Rows[i].ItemArray;
}

dict.Add("response", arr);

JavaScriptSerializer json = new JavaScriptSerializer();
return json.Serialize(dict);
}

解决方案

This is one of the reasons you should avoid using "var" until you're more experienced with .net. EF thinks your SP is what is known as a scalar procedure, ie it only returns a single value, not a result set\table. So the return value is int so your dq variable is int, and you can only loop on variables that are collections. Have a look at how the SP is create in EF, maybe delete it from the EF model and re-add it.


ScottGu&#39;s Blog - LINQ to SQL (Part 6 - Retrieving Data Using Stored Procedures)[^]


这篇关于在LINQ中调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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