【问题标题】:select * only return one valueselect * 只返回一个值
【发布时间】:2012-09-21 12:51:19
【问题描述】:

我想从表 Account 中选择多个(所有)值。

string query = "SELECT * FROM Account";
        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(query, connection);
        SqlDataReader reader;
        connection.Open();
        reader = command.ExecuteReader();
        reader.Read();
        label1.Text = reader["PasswordHash"].ToString();
        connection.Close();

为什么总是只返回第一行。实际上它返回一行,因为如果我在 where 子句中设置类似where id = 2 and id = 3 它仍然只返回一个值。 表有多个值,我在 Management Studio 中检查过,查询运行正常。

提前致谢。

【问题讨论】:

  • 我很好奇:如果您想从结果集中读取多行,您希望 label1.Text 完成后的样子?
  • 因为你只给reader.Read()打了一次电话?!?!?!?该调用读取结果集的 一行 - 仅此而已。
  • 这只是练习以完全掌握 sql server 功能。标签是最容易开始的事情。

标签: sql-server select ado.net


【解决方案1】:

因为你没有遍历查询结果,所以它只显示一个结果。

string query = "SELECT * FROM Account";
    SqlConnection connection = new SqlConnection(connectionString);
    SqlCommand command = new SqlCommand(query, connection);
    SqlDataReader reader;
    connection.Open();
    reader = command.ExecuteReader();
    While(reader.Read())
{
    label1.Text += " " +reader["PasswordHash"].ToString();
}
    connection.Close();

上面的代码循环遍历查询结果,并在分配给label1.text 的串联字符串中为您提供所需的内容。你也可以通过在while循环中插入Console.WriteLine(reader["PasswordHash"].ToString());来查看结果

【讨论】:

    【解决方案2】:

    你应该这样做

    while (reader.Read())
        // process information
    

    您需要迭代您检索到的所有信息。

    旁注:在 SqlConnection、SqlCommand 和 SqlDataReader 上使用 using 语句来确保对象得到正确处理。

    【讨论】:

      【解决方案3】:

      阅读器一次只能前进一条记录。您需要循环遍历结果集:

      while (reader.Read()) {
          // do something with the current row by accessing reader[]
      }
      reader.Close();
      

      有更好的方法来构建您的代码,但这说明了您遗漏的一点并且需要最少的更改。

      【讨论】:

        【解决方案4】:

        你需要循环,像这样:

        SqlDataReader reader = command.ExecuteReader();
        
        // Call Read before accessing data.
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}",
                reader[0], reader[1]));
        }
        

        MSDN 文档中有一个例子:

        http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

        【讨论】:

          【解决方案5】:

          reader.Read() 行将获得下一行,因此您可以使用 while(reader.Read()) 遍历行。

          【讨论】:

            【解决方案6】:

            查看您的代码,我的建议是使用其他对象来添加代码。标签文本不是合适的选择。

            尝试在列表中使用 foreach 循环来检索已返回的所有数据。

            【讨论】:

              【解决方案7】:

              你需要一个while循环;

              while(reader.Read()) {
                         Console.WriteLine("{0}", reader[0]);
                      }
              

              【讨论】:

                【解决方案8】:

                使用where id = 2 and id = 3 将返回零结果,因为 id=2 和 id=3 是互斥的。 where id = 2 or id = 3 可以工作。

                while (reader.Read()) { /*Do stuff with current row*/ }
                

                可以对结果进行迭代

                【讨论】:

                • 我在想“或”,但我写了“和”:)
                【解决方案9】:

                您需要循环和数据阅读器是您的代码的最佳方式,对于示例,如下所示:

                SqlDataReader myReader = myCommand.ExecuteReader();
                // Always call Read before accessing data.
                while (myReader.Read()) {
                   Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
                }
                // always call Close when done reading.
                myReader.Close();
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-12-30
                  • 1970-01-01
                  • 2012-11-13
                  • 2019-03-22
                  • 2014-10-18
                  • 2018-05-14
                  相关资源
                  最近更新 更多