我怎麼能只讀一條記錄,當我想要所有記錄它的罰款,但是當我需要@id的特定記錄,我的while循環jums出?如何使用ado.net reader?
[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");
}
什麼是你的問題? – 2012-01-12 18:24:16
你的代碼看起來不錯,確保你的查詢實際上返回任何行。 – 2012-01-12 18:26:43
@D Stanley,我如何只讀一條記錄,當我希望所有記錄都很好時,但是當我需要@id的特定記錄時,我的while循環出現了? – 2012-01-12 18:26:48