嗨,我遇到了关于程序窗口警报的麻烦。它总是显示“没有在数据库中找到记录”,但它成功删除了该行。在这里要一只手。谢谢!
try
{
con.Open();
string Sql = "DELETE FROM contacts WHERE LastName LIKE '" + txtbx_delete.Text + "'";
MySqlDataAdapter da = new MySqlDataAdapter(Sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Delete successful! Check display!')", true);
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('No record with that last name found!')", true);
}
}
答案 0 :(得分:4)
您需要使用ExecuteNonQuery()
代替适配器,根据您可以抛出消息,它会为您提供受影响的行数;并且可能parameterized queries是避免注射的更好选择(因为你正在处理删除更加无能为力)。
string Sql = "DELETE FROM contacts WHERE LastName LIKE @delete";
using(MySqlCommand cmd = new MySqlCommand(readCommand))
{
cmd.Parameters.Add(new MySqlParameter("@delete", txtbx_delete.Text));
int result= m.ExecuteNonQuery();
//result holds number of rows affected
if (result>0)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Delete successful! Check display!')", true);
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('No record with that last name found!')", true);
}
}
始终尝试使用using
语句正确处置对象。
您也可以尝试连接,它会在使用后立即断开连接。
答案 1 :(得分:2)
这是因为您没有获取表格。您只是在运行删除查询。