【发布时间】: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