简体   繁体   English

如何从存储过程中获取多行?

[英]How to get multiple rows from stored procedure?

I have a method for adding values to the database for all operations. 我有一种为所有操作将值添加到数据库的方法。

If this is selected from the database and this select return more rows from the database, how can I get the rows and store in an array? 如果从数据库中选择了此选项,并且此选择返回了数据库中的更多行,那么如何获取行并存储在数组中?

This is the method code : 这是方法代码:

public void ExcuteProcedure(string procName, List<SqlParameter> procparams)
{
            try
            {
                SqlConnection mycon = new SqlConnection(connectionString);
                mycon.Open();
                SqlCommand mycom = new SqlCommand();
                mycom.Connection = mycon;
                mycom.CommandText = procName;
                mycom.CommandType = CommandType.StoredProcedure;
                foreach (var item in procparams)
                {
                    SqlParameter myparm = new SqlParameter();
                    myparm.ParameterName = item.ParameterName;
                  //  myparm.SqlDbType = item.SqlDbType;
                    myparm.Value = item.Value;
                    mycom.Parameters.Add(myparm);
                }
              var n= mycom.ExecuteScalar();

                mycon.Close();
            }
            catch (SqlException e)
            {
                Console.WriteLine("Error Number is : " + e.Number);
                Console.WriteLine("Error Message is : " + e.Message);
            }
}

You need to call mycom.ExecuteReader() , which will give you a SqlDataReader which can read through the results. 您需要调用mycom.ExecuteReader() ,这将为您提供一个可以读取结果的SqlDataReader

Call Read() to advance through the rows. 调用Read()进行。

It never ceases to amaze me the number of times I see devs trying to abstract away simple database connectivity; 我看到开发人员试图抽象出简单的数据库连接的次数从未间断。 and the myriad of ways they inevitably screw it up. 以及不可避免的各种方式

The following may sound mean, but it needs said: Clean up your code, it leaks like a sieve. 以下内容可能听起来很卑鄙,但需要说明:清理代码,它像筛子一样泄漏。 Using clauses around the connection and command objects are pretty much mandatory. 在连接和命令对象周围使用子句几乎是强制性的。 As it stands if you forget a single parameter or put in a bad value you will leak connections. 就目前而言,如果您忘记了单个参数或设置了错误的值,则会泄漏连接。 Once the connection pool is filled up your app will crash in all sorts of interesting, and usually hard to debug, ways. 连接池填满后,您的应用将以各种有趣的方式崩溃,并且通常很难调试。

Next, if you aren't sure how to properly get records back from a database then you probably shouldn't try to abstract the code calling your procedures. 接下来,如果不确定如何正确地从数据库取回记录,则可能不应该尝试抽象化调用过程的代码。 Either use a lightweight ORM like Dapper or learn how what you are doing will ultimately involve a lot of extraneous code that the next developer on your project will want to rip out. 使用像Dapper这样的轻量级ORM或了解您的工作最终将涉及很多无关的代码,而您项目中的下一个开发人员将希望提取这些代码。

/rant over. /结束。

Getting back to the question: ExecuteScalar returns a single value. 回到问题: ExecuteScalar返回单个值。 You need to use ExecuteReader . 您需要使用ExecuteReader I'd suggest that you simply take the results of the reader, stuff it into a datatable and pass that back to the calling code. 我建议你干脆把阅读器的结果,将它填满到一个datatable ,并传递回调用代码。

var n = mycom.ExecuteScalar();

Scalar: an atomic quantity that can hold only one value at a time 标量:一次只能容纳一个值的原子量

  • Return a DataReader instead, and iterate through its rows 而是返回一个DataReader ,并遍历其行
  • Fill a DataSet by using a DataAdapter (this is more appropriate if you have multiple tables in the result set). 通过使用DataAdapter填充DataSet (如果结果集中有多个表,则更合适)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM