何をしようとしているのは、SQL Serverデータベース内のレコードを削除すると、数量がゼロより大きいかどうかを最初にチェックし、そうでなければレコードを削除しません。私はそれが今働いていると思いますが、私が何をしてもこのエラー "DataReaderは最初に閉じなければなりません"を得ています。どんな助けもありがとう。このエラーを解決するにはどうすればよいですか?最初にDataReaderを閉じる必要があります
private void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=inventory2;Integrated Security=True");
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT Quantity FROM Items WHERE (Barcode = '" + dataGridView1.CurrentRow.Cells[0].Value + "')", cn);
SqlDataReader quantityRdr = null;
quantityRdr = cmd.ExecuteReader();
while (quantityRdr.Read())
{
string squantity = quantityRdr["Quantity"].ToString();
int x = Int32.Parse(squantity);
if (x > 0)
{
MessageBox.Show("You can't delete this record");
}
else
{
DialogResult r = MessageBox.Show("Are you sure you want to delete this record?", "Delete", MessageBoxButtons.YesNo);
if (r == DialogResult.Yes)
{
SqlCommand cmd1 = new SqlCommand(@"DELETE FROM Items where (Barcode = '" + dataGridView1.CurrentRow.Cells[0].Value + "')", cn);
MessageBox.Show("Item deleted!");
cmd1.ExecuteNonQuery();
fill();
}
else
{
MessageBox.Show("invalid");
}
}
}
どの行にエラーがありますか?また、別のノートでは、DataReaderを先に閉じないでください。 cmd1.executenonquery();の –
;ライン。しかし、今度はその答えが – FutureDev
のおかげで、大丈夫です。Sql Injectionの素晴らしいスキーマです。私は列の名前を知る必要はありません。 _ 'OR 1 = 1'と入力するだけで十分です。 - _できるだけ早くSql Injectionを検索してください。 – Steve