如果我的存储过程是
Alter dbo.Testing
Select userid from masterdb
where employed = 'Yes'
Select Count(*) from leftfield
where pin is not null
如何将每个查询的返回结果存储在单独的数据集中?
伪代码:
firstdataset = Select userid from masterdb where employed = 'Yes'
seconddataset = select count(*) from leftfield where pin is not null
修改
我使用它将1个结果集从存储过程返回到C#数据集。是否可以返回超过1?
public DataSet RunStoredProc(string databaseConnection)
{
ds = new DataSet();
DSqlQueryBuilder = new StringBuilder();
SqlQueryBuilder.Append("exec dbo.StoredProc ");
SqlConnection = new SqlConnection(connectionString);
SqlCommand = new SqlCommand(sqlQuery, SqlConnection);
SqlConnection.Open();
SqlCommand.CommandTimeout = 0;
ds = new DataSet();
SqlDataAdapter = new SqlDataAdapter(SqlCommand);
SqlDataAdapter.Fill(ds, "Data");
return ds;
}
编辑#2
public DataSet RunStoredProc(string databaseConnection)
{
ds = new DataSet();
DSqlQueryBuilder = new StringBuilder();
SqlQueryBuilder.Append("exec dbo.StoredProc ");
//Error 1 On Line Below
ds = ExecuteSqlQuery(databaseConnection, SqlQueryBuilder.ToString());
return ds;
}
public List<DataTable> RunStoredProc(string databaseConnection)
{
var dataTables = new List<DataTable>();
DSqlQueryBuilder = new StringBuilder();
SqlQueryBuilder.Append("exec dbo.StoredProc ");
SqlConnection = new SqlConnection(connectionString);
SqlCommand = new SqlCommand(sqlQuery, SqlConnection);
var reader = SqlCommand.ExecuteReader();
DataTable dt1 = new DataTable();
dt1.Load(reader);
dataTables.Add(dt1);
DataTable dt2 = new DataTable();
dt2.Load(reader);
dataTables.Add(dt2);
return dataTables;
}
错误#1
无法隐式转换类型&#39; System.Collections.Generic.List&#39;到&#39; System.Data.DataSet&#39;
答案 0 :(得分:2)
你可以这样做:
subdomain.me
**编辑添加连接数据库的功能**
获取您的值的示例函数:
post.subdomain.me:3000
答案 1 :(得分:2)
您正在寻找SqlReader.NextResult()方法,所以:
public List<DataTable> RunStoredProc(string databaseConnection)
{
var dataTables = new List<DataTable>();
DSqlQueryBuilder = new StringBuilder();
SqlQueryBuilder.Append("exec dbo.StoredProc ");
SqlConnection = new SqlConnection(connectionString);
SqlCommand = new SqlCommand(sqlQuery, SqlConnection);
var reader = SqlCommand.ExecuteReader();
var dt1 = new DataTable().Load(reader);
dataTables.Add(dt1);
reader.NextResult();
var dt2 = new DataTable().Load(reader);
dataTables.Add(dt1);
// Make sure to really use usings here to ensure all resources are being
closed
return dataTables;
}
编辑:编辑从DataSet更改为DataTable,使Load函数按照我指示的方式工作(不幸的是从内存中)。这将返回一个DataTables列表(可能更适当地指示返回的内容)。坦率地说,我不会使用它,更喜欢EntityFramework和强类型结果(但我当然不知道整个用例,性能要求等)。
答案 2 :(得分:0)
您需要一个函数来返回结果,而不是存储过程。存储过程旨在仅返回状态值。编写两个函数,每个函数返回一个非标量值,实际上是一个数据集。
答案 3 :(得分:0)
我最终使用以下解决方案来解决我的问题。我的SQL存储过程看起来像这样
Select Top 1 field1 from table1
Select Top 1 field1 from table1
这是我使用的C#(没有html来创建gridview)
protected void PopulateTestGrids()
{
DataSet ds = new DataSet();
ds = RunStoredProc();
DataTable tableA = ds.Tables[0];
DataTable tableB = ds.Tables[1];
GridView21.DataSource = tableA;
GridView21.DataBind();
GridView31.DataSource = tableB;
GridView31.DataBind();
}
public DataSet RunStoredProc(string databaseConnection)
{
ds = new DataSet();
DSqlQueryBuilder = new StringBuilder();
SqlQueryBuilder.Append("exec dbo.StoredProc ");
ds = ExecuteSqlQuery(databaseConnection, SqlQueryBuilder.ToString());
return ds;
}
public DataSet ExecuteSqlQuery(string databaseConnection, string sql)
{
try
{
System.Configuration.ConnectionStringSettings connstring = System.Configuration.ConfigurationManager.ConnectionStrings["abcddb"];
using (SqlConnection conn = new SqlConnection(connstring.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = sqlQuery;
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(_dataSet);
conn.Close();
}
}
return _dataSet;
}
catch (Exception exception) { throw exception; }
}