2014-08-30 3 views
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."); 
      } 
     } 
    } 

답변

1

필자는 이전에 이것을 보았지만 SQL Engine이 자체적으로 업데이트되는 것처럼 보입니다. @parm이 열과 같기 때문에 sql은 나와 함께 나를 업데이트한다고 말합니다.

는 SQL 열에 "pProductName을"이없는 약간 해

UPDATE Products SET productName = @pProductName WHERE productID = @pPproductID"; 
myCommand2.Parameters.AddWithValue("@pProductName", txtProductName.Text); 
myCommand2.Parameters.AddWithValue("@pProductID", lblProductID.Text); 

으로이 방법을 당신의 매개 변수의 이름을 변경 시도, 수정과에 - 사실 "pProductID"와 마찬가지로 ... 매개 변수로 이동합니다 . 매우 미묘하지만, 아마도 당신이 마주 치고있는 것입니다. 또한 어떤 데이터베이스에 연결하고 있습니까? 다른 엔진은 매개 변수 전달을 위해 다른 토큰 문자를 사용합니다 ... "?", "@", ":"로 알고 있습니다.

+0

SQL Server를 사용하고 있습니다. 나는 이것을 시도했지만 결과는 같습니다. 이것은 다른 방법으로는 잘 작동하기 때문에 나를 미치게 만든다. – Katp00p

+0

@ Katp00p, int RowsUpdated = cmd.executenonquery ...를 추가 한 다음 rowsUpdated 값을 messagebox에 추가하십시오. 예상대로 또는 제로 기록으로 돌아 오는 것입니까? – DRapp

+0

문제를 파악했습니다 ... 쿼리와 관련이 없습니다. lblProductID에 저장된 값이었습니다. 도와 주셔서 감사합니다. 장래에 사람들을 혼동하지 않도록이 질문을 삭제하겠습니다. – Katp00p

관련 문제