如何获取存储过程返回的两个表

时间:2017-12-20 03:57:12

标签: c# sql-server linq-to-sql

我的方法:

[Function(Name = "get_values")]
[ResultType(typeof(Values_Result))]
public IMultipleResults getvaluesresult([Parameter(DbType = "Int")] System.Nullable<int> Id1, [Parameter(DbType = "VarChar(100)")] string Id2)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), Id1, Id2);
    return ((IMultipleResults)(result.ReturnValue));
}

方法调用:

var resultValues = cont1.getvaluesresult(Convert.ToInt32(clsSession.id1), "101");
var resultValuesList = resultValues.GetResult<Values_Result>().ToList<Values_Result>();

if (resultValuesList != null && resultValuesList.Count > 0)
{
    var dataTable = resultValuesList.ToDataTable();
    // performing some tasks
}

我的存储过程如下所示:

CREATE  PROCEDURE get_values
    (@Id2 VARCHAR(50),  
     @Id1 INT)
AS                  
BEGIN 
    DECLARE @temp TABLE (id INT)

    INSERT INTO @temp 
        (SELECT id FROM table1)

    SELECT * FROM table2
    SELECT * FROM @temp
END

我想知道如何从存储过程中获取两个返回的表。

我不想将存储过程中的表作为一个

加入

3 个答案:

答案 0 :(得分:2)

使用ADO.NET:

SqlConnection con = new SqlConnection("YourConnection String");
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
cmd = new SqlCommand("get_values", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id2", id); // if you have parameters.
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();

// ds.Tables[0] your first select statement
// ds.Tables[1] your second select statement

使用实体框架:

// Create a SQL command to execute the sproc
var cmd = db.Database.Connection.CreateCommand(); // db is your data context
cmd.CommandText = "[dbo].[get_values]";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id2", id); // if you have parameters.

db.Database.Connection.Open();
var reader = cmd.ExecuteReader();

// Read Table2 from the first result set
var table2 = ((IObjectContextAdapter)db)
            .ObjectContext
            .Translate<Table2Model>(reader, "table2", MergeOption.AppendOnly);   

// Move to second result set and read @temp
reader.NextResult();
var temp = ((IObjectContextAdapter)db)
            .ObjectContext
            .Translate<TempModel>(reader, "temp", MergeOption.AppendOnly); 
// I forgot if you need to use @temp or temp only

答案 1 :(得分:1)

您需要再次为第二个表调用GetResult函数。

如果存储过程导致多个表结果,则应再次调用GetResult函数以检索下一个表结果。

        var resultValues = cont1.getvaluesresult(Convert.ToInt32(clsSession.id1), "101");

        // For table2
        var resultValuesList1 = resultValues.GetResult<Values_Result>().ToList<Values_Result>();

        if (resultValuesList1 != null && resultValuesList1.Count > 0)
        {
            var dataTable = resultValuesList1.ToDataTable();
            // performing some tasks
        }

        // For temp table
        var resultValuesList2 = resultValues.GetResult<Values_Result>().ToList<Values_Result>();

        if (resultValuesList2 != null && resultValuesList2.Count > 0)
        {
            var dataTable = resultValuesList2.ToDataTable();
            // performing some tasks
        }

答案 2 :(得分:0)

var resultValues = cont1.getvaluesresult(Convert.ToInt32(clsSession.id1), 
"101");
var resultValuesList = resultValues.GetResult<Values_Result>
().ToList<Values_Result>();
if (resultValuesList != null && resultValuesList.Count > 0)
 {
DataSet ds = resultValuesList.ToDataTable();
 // performing some tasks
 }

此数据集可以存储多个表,您可以使用它