通过LiNQ从SQL获得多个结果集
我有一个存储过程 pr_uuuuGetArchiveData 从表1中选择* 从表2中选择* 从表3中选择* 我想把这个结果集放到一个数据集中。或访问三个select查询的值!! 我有一个DBML文件,当我在其中拖放时,存储过程会生成如下代码:-通过LiNQ从SQL获得多个结果集,linq,asp.net-mvc-3,Linq,Asp.net Mvc 3,我有一个存储过程 pr_uuuuGetArchiveData 从表1中选择* 从表2中选择* 从表3中选择* 我想把这个结果集放到一个数据集中。或访问三个select查询的值!! 我有一个DBML文件,当我在其中拖放时,存储过程会生成如下代码:- global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.pr___GetArchiveData")] public ISingleResult<pr__
global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.pr___GetArchiveData")]
public ISingleResult<pr___GetArchiveDataResult> pr___GetArchiveData([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="UniqueIdentifier")] System.Nullable<System.Guid> projectID)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), projectID);
return ((ISingleResult<pr__Project_pr___GetArchiveData>)(result.ReturnValue));
}
global::System.Data.Linq.Mapping.FunctionAttribute(Name=“dbo.pr\uuuuu GetArchiveData”)]
公共ISingleResult pr_uuuugetArchiveData([global::System.Data.Linq.Mapping.ParameterAttribute(DbType=“UniqueIdentifier”)]System.Nullable projectID)
{
IExecuteResult=this.ExecuteMethodCall(this,((MethodInfo)(MethodInfo.GetCurrentMethod()),projectID);
返回((IsingResult)(result.ReturnValue));
}
在代码MVC3 Architecture+LINQ中,我编写了一段代码,以获得如下结果集:-
using (HBDataContext hb = new HBDataContext())
{
System.Data.DataSet ds = new System.Data.DataSet();
String connString = connString;
var conn = new System.Data.SqlClient.SqlConnection(connString);
var cmd = conn.CreateCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "pr__GetArchiveData";
cmd.Connection.Open();
var mReader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
//var reader = cmd.ExecuteReader();
//using (System.Data.SqlClient.SqlDataReader mReader = cmd.ExecuteReader())
//{
// while (mReader.Read())
//{
// mReader.Read();
var tbl1 = hb.Translate<tbl1 >(mReader).ToList();
// mReader = cmd.ExecuteReader();
mReader.NextResult();
var tbl2 = hb.Translate<tbl2 >(mReader).ToList();
mReader.NextResult();
var tbl3 = hb.Translate<tbl3>(mReader).ToList();
// }
// }
}
使用(HBDataContext hb=new HBDataContext())
{
System.Data.DataSet ds=新的System.Data.DataSet();
字符串connString=connString;
var conn=new System.Data.SqlClient.SqlConnection(connString);
var cmd=conn.CreateCommand();
cmd.CommandType=System.Data.CommandType.StoredProcess;
cmd.CommandText=“pr\uu GetArchiveData”;
cmd.Connection.Open();
var mReader=cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
//var reader=cmd.ExecuteReader();
//使用(System.Data.SqlClient.SqlDataReader mReader=cmd.ExecuteReader())
//{
//while(mReader.Read())
//{
//mReader.Read();
var tbl1=hb.Translate(mReader.ToList();
//mReader=cmd.ExecuteReader();
mReader.NextResult();
var tbl2=hb.Translate(mReader.ToList();
mReader.NextResult();
var tbl3=hb.Translate(mReader.ToList();
// }
// }
}
但在运行时,它会抛出错误-
“在读卡器关闭时调用NextResult的尝试无效。”
我不确定我错在哪里
我试着把它当工具使用
(mReader.Read())
好心的建议 ISingResult的代码生成器不会提供多个结果集 尝试添加自己的
IMultipleResults
wrapper-请参阅中的教程
[global::System.Data.Linq.Mapping.FunctionAttribute(Name=“dbo.pr\uu Home\u GetArchiveData”)]
公共ISingleResult pr\uuuuuuHome\uGetArchiveData([global::System.Data.Linq.Mapping.ParameterAttribute(Name=“AlbumID”,DbType=“UniqueIdentifier”)]System.Nullable AlbumID)
{
IExecuteResult=this.ExecuteMethodCall(this,((MethodInfo)(MethodInfo.GetCurrentMethod()),albumID);
返回((IsingResult)(result.ReturnValue));
}
using (HBDataContext hb = new HBDataContext())
{
using (System.Data.Linq.IMultipleResults _results = hb.pr__Home_GetArchiveData(model.HomeID))
{
List<tbl1> _tbl1= _results.GetResult<tbl1>().ToList();
List<tbl2> _tbl2= _results.GetResult<tbl2>().ToList();
List<tbl3> _tbl3= _results.GetResult<tbl3>().ToList();
List<tbl4> _tbl4= _results.GetResult<tbl4>().ToList();}}
使用(HBDataContext hb=new HBDataContext())
{
使用(System.Data.Linq.IMultipleResults\u results=hb.pr\u Home\u GetArchiveData(model.HomeID))
{
列表_tbl1=_results.GetResult().ToList();
列表_tbl2=_results.GetResult().ToList();
列表_tbl3=_results.GetResult().ToList();
列表_tbl4=_results.GetResult().ToList();}
您将从存储过程中获得Select查询的值…您是否考虑过使用SP函数pr_uuuugetArchiveData并仅返回ISingResult?谢谢Stuart。成功了。只需使用建议的链接进行一点修改:)