0
다음 코드가 작동하지만 제품이 업데이트되었음을 알리는 messagebox가 표시되지만 데이터베이스는 변경되지 않았습니다.C# executenonquery가 데이터베이스를 업데이트하지 않습니다.
동일한 데이터베이스에서 작동하는 다른 방법으로 복사하여 붙여 넣었으므로 해당 메서드가 올바르게 작동합니다.
private void btnProductSave_Click(object sender, EventArgs e)
{
if (txtProductName.Text != "")
{
var confirmResult = MessageBox.Show("Are you sure you want to save changes to this product?", "Confirm Save", MessageBoxButtons.YesNo);
if (confirmResult == DialogResult.Yes)
{
SqlConnection con = null;
con = new SqlConnection(ConfigurationManager.ConnectionStrings["PVAdmin.Properties.Settings.PricerConnectionString"].ConnectionString);
string sql2 = "UPDATE Products SET productName = @productName WHERE productID = @productID";
SqlCommand myCommand2 = new SqlCommand(sql2, con);
myCommand2.Parameters.AddWithValue("@productName", txtProductName.Text);
myCommand2.Parameters.AddWithValue("@productID", lblProductID.Text);
con.Open();
myCommand2.ExecuteNonQuery();
con.Close();
MessageBox.Show("Product saved.");
this.LoadProduct((int)cboProductSelect.SelectedValue);
}
else
{
MessageBox.Show("No changes were saved.");
}
}
}
SQL Server를 사용하고 있습니다. 나는 이것을 시도했지만 결과는 같습니다. 이것은 다른 방법으로는 잘 작동하기 때문에 나를 미치게 만든다. – Katp00p
@ Katp00p, int RowsUpdated = cmd.executenonquery ...를 추가 한 다음 rowsUpdated 값을 messagebox에 추가하십시오. 예상대로 또는 제로 기록으로 돌아 오는 것입니까? – DRapp
문제를 파악했습니다 ... 쿼리와 관련이 없습니다. lblProductID에 저장된 값이었습니다. 도와 주셔서 감사합니다. 장래에 사람들을 혼동하지 않도록이 질문을 삭제하겠습니다. – Katp00p