【问题标题】:How to use ado.net reader? [closed]如何使用 ado.net 阅读器? [关闭]
【发布时间】:2012-01-12 18:22:42
【问题描述】:

我怎样才能只读取一条记录,当我希望所有记录都正常时,但当我需要带有@id 的特定记录时,我的 while 循环会跳出?

[HttpGet]
public ActionResult DeleteArticle(int ProductID)
{
    int id = ProductID;

    if (ModelState.IsValid)
    {
           string connStr = ConfigurationManager.ConnectionStrings["AdminServices"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(connStr))
            {
                connection.Open();

               //delete from database
                using (SqlCommand command = new SqlCommand("DELETE FROM MyTable  WHERE id = @id", connection))
                {
                    command.Parameters.AddWithValue("@id", id);
                    command.ExecuteNonQuery();
                }

                 //read imagePathe from Database from database
                using (SqlCommand command = new SqlCommand("SELECT * FROM MyTable WHERE id = @id", connection))
                {

                    command.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = id;
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read()) // --> here it skips while loop ????
                    {

                        string ImagePath = (string)reader["DbImagePath"];

                    }

                }

            }
        }
        return RedirectToAction("Index", "Admin");
    }

【问题讨论】:

  • 您的代码看起来不错,请确保您的查询实际上返回了任何行。
  • @D Stanley,当我希望所有记录都正常但当我需要带有@id 的特定记录时,我如何只读取一条记录?
  • 您要删除它然后再阅读它吗?您使用的是相同的产品 ID
  • @JohnGibb,我做了,我检查是否有一个 id = x,并且有,但它接缝读者不会阅读???
  • @LukeHutton, 嗯...我猜读者没有阅读因为记录已经被删除.....

标签: c# .net sql asp.net-mvc-3 ado.net


【解决方案1】:

while (reader.Read()) { } 将在没有更多记录可读取时中断。由于您的第一个命令会删除由id 标识的记录,因此该ID 将永远不会读取任何内容。

【讨论】:

    【解决方案2】:

    如果删除第一个查询的行,第二个查询应该如何返回任何内容?

    【讨论】:

    • 谢谢大家,我想我应该在这个上切换操作:-)
    • 有时我们过度思考问题而忽略了明显的错误——我们都经历过:)
    【解决方案3】:

    您正确使用了SqlDataReader。它应该跳过该 while 循环的唯一原因是如果您没有取回任何记录。

    【讨论】:

      【解决方案4】:

      这里是示例

      private static void ReadOrderData(string connectionString)
      {
          string queryString =
              "SELECT OrderID, CustomerID FROM dbo.Orders;";
      
          using (SqlConnection connection =
                     new SqlConnection(connectionString))
          {
              SqlCommand command =
                  new SqlCommand(queryString, connection);
              connection.Open();
      
              SqlDataReader reader = command.ExecuteReader();
      
              // Call Read before accessing data.
              while (reader.Read())
              {
                  Console.WriteLine(String.Format("{0}, {1}",
                      reader[0], reader[1]));
              }
      
              // Call Close when done reading.
              reader.Close();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-03
        • 1970-01-01
        • 2011-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-23
        相关资源
        最近更新 更多