【问题标题】:Return 2 Results From SQL从 SQL 返回 2 个结果
【发布时间】:2015-12-10 20:02:19
【问题描述】:

如果我有一个存储过程是

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
无法将类型“System.Collections.Generic.List”隐式转换为“System.Data.DataSet”

【问题讨论】:

标签: c# webforms sql-server-2008-r2


【解决方案1】:

你可以这样做:

Alter dbo.Testing

-- Declare your variables
Declare @userId AS INT;
Declare @count AS INT;

-- Set the value
Select @userId = userid from masterdb where employed = 'Yes';
Select @count = Count(1) from leftfield where pin is not null;

-- Return the values
Select @userId, @count;

**EDIT 添加函数连接数据库**

获取值的示例函数:

static DataSet sqlTest(string connectionString)
{
    using (var sqlConnection = new SqlConnection(connectionString))
    {
        sqlConnection.Open();
        var sqlCommand = new SqlCommand("exec dbo.StoredProc", sqlConnection);
        var dataSet = new DataSet();
        var sqlDataAdapter = new SqlDataAdapter(sqlCommand);
        sqlDataAdapter.Fill(dataSet, "Data");

        // you can access your values like that:
        var userId = dataSet.Tables["Data"].Rows[0][0];
        var count = dataSet.Tables["Data"].Rows[0][1];

        return dataSet;
    }
}

【讨论】:

  • 如何将 C# 数据集分配给每个返回的 SQL 变量?
【解决方案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 和强类型结果(但我当然不知道整个用例、性能要求等)。

【讨论】:

  • 是的,就是这个想法。只要 sproc 在执行结束时返回两个结果。让我知道这是否适合你
  • 我得到 2 个编译错误 1) '方法 'Load' 没有重载需要 1 个参数' & 2) 不能将类型 'List' 隐式转换为 'System.Data .DataSet'
  • 嗨,很抱歉。事实证明,从内存中编码总是不好的 :) 在这种情况下,您真的需要数据集还是数据表会这样做?我问的是 DataTable.Load 方法实际上只需要一个 DataReader ,而在您的情况下,您实际上只是在加载两个 DataTables。我现在将编辑我的答案(您可能需要更改调用方法的方式,这可能是错误原因之一)。一定要展示你是如何调用它的,以防你也需要帮助。
  • 在 var dt1 和 var dt2 行上,我收到一个错误,即无法将 void 分配给隐式类型的局部变量。我将使用您的语法以及我的调用方式编辑我的原始帖子,因为我的调用也会引发错误。
【解决方案3】:

您需要一个函数来返回结果,而不是存储过程。存储过程旨在仅返回状态值。编写两个函数,每个函数都返回一个非标量值,它本质上是一个数据集。

【讨论】:

    【解决方案4】:

    我最终使用以下解决方案来解决我的问题。我的 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; }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 2017-01-03
      相关资源
      最近更新 更多