存储过程里有若干个select和返回参数:
例:存储过程
ALTER PROCEDURE RunSQLS
AS
Begin
Select * From table_1
Select * From table_2
Select * From table_3
End我写了下面这段代码,帮忙看下是否可行? public DataTable ExecuteProcedure_ReturnDataSet(string Procedure_Name)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["DBconnStrings"]))
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(Procedure_Name, con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
con.Open();
da.SelectCommand = cmd;
da.Fill(ds);
return ds;
}
catch(SqlException er)
{
throw new Exception(er.Message);
}
finally
{
cmd.Dispose();
ds.Dispose();
da.Dispose();
con.Close();
}
}
}然后调用能否用下面这种方式: DataSet ds = ExecuteProcedure_ReturnDataSet("RunSQLS"); BindID_1.DataSource = ds.Tables[0];
BindID_1.DataBind();
BindID_2.DataSource = ds.Tables[1];
BindID_2.DataBind();
BindID_3.DataSource = ds.Tables[2];
BindID_3.DataBind();
例:存储过程
ALTER PROCEDURE RunSQLS
AS
Begin
Select * From table_1
Select * From table_2
Select * From table_3
End我写了下面这段代码,帮忙看下是否可行? public DataTable ExecuteProcedure_ReturnDataSet(string Procedure_Name)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["DBconnStrings"]))
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(Procedure_Name, con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
con.Open();
da.SelectCommand = cmd;
da.Fill(ds);
return ds;
}
catch(SqlException er)
{
throw new Exception(er.Message);
}
finally
{
cmd.Dispose();
ds.Dispose();
da.Dispose();
con.Close();
}
}
}然后调用能否用下面这种方式: DataSet ds = ExecuteProcedure_ReturnDataSet("RunSQLS"); BindID_1.DataSource = ds.Tables[0];
BindID_1.DataBind();
BindID_2.DataSource = ds.Tables[1];
BindID_2.DataBind();
BindID_3.DataSource = ds.Tables[2];
BindID_3.DataBind();
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。编译器错误信息: CS0029: 无法将类型“System.Data.DataSet”隐式转换为“System.Data.DataTable”源错误: da.SelectCommand = cmd;
da.Fill(ds);
return ds; 出错行!
}
catch(SqlException er)
改成 public DataSet ExecuteProcedure_ReturnDataSet(string Procedure_Name)呵呵。。可以了。。谢谢大家