2017-06-20 6 views
2

私はこの質問が以前に尋ねられたことは知っていますが、なぜ私のコードがデータベースに更新されないのかについての答えをまだ得られません。このコードにはエラーはありませんが、データをデータベースに更新しません。誰でも私を助けてください。ここに私のコードは次のとおりです。データベースに更新する方法

private void button1_Click_1(object sender, EventArgs e) 
    { 
     con.Open(); 
     bool exists2; 
     using (SqlCommand cmd2 = new SqlCommand("select count(*) from [m_emp_photo] where [email protected]", con)) 
      { 
       cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text); 
       cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text); 
       exists2 = (int)cmd2.ExecuteScalar() > 0; 
      } 

     if (exists2) 
     { 
      string query2 = ""; 
      //query2 = "update m_emp_photo set [email protected] where [email protected]"; 

      query2 = "UPDATE m_emp_photo set [email protected] where [email protected]"; 

      SqlCommand cmd2; 
      cmd2 = new SqlCommand(query2, con); 

      cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text); 
      MessageBox.Show("Changes has been saved!"); 
     } 

     else 
     { 
      MessageBox.Show("No record found"); 
     } 
     con.Close(); 
    } 

答えて

0

Ifステートメントで 使用をあなたのSqlCommandオブジェクトを実行するための最初のを逃したと思いますcmd2は、@pathパラメータを追加する必要はありません。 2番目のcmd2には、@empnoパラメータを追加してExecuteNonQueryを実行する必要があります。 私はそれがあなたのために働くことを願っています。

private void button1_Click_1(object sender, EventArgs e) 
{ 
    con.Open(); 
    bool exists2; 
    using (SqlCommand cmd2 = new SqlCommand("select count(*) from [m_emp_photo] where [email protected]", con)) 
    { 
     cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text); 
     //cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text); 
     exists2 = (int)cmd2.ExecuteScalar() > 0; 
    } 

    if (exists2) 
    { 
     string query2 = ""; 
     //query2 = "update m_emp_photo set [email protected] where [email protected]"; 

     query2 = "UPDATE m_emp_photo set [email protected] where [email protected]"; 

     SqlCommand cmd2; 
     cmd2 = new SqlCommand(query2, con); 

     cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text); 
     cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text); 

     cmd2.ExecuteNonQuery(); 

     MessageBox.Show("Changes has been saved!"); 
    } 

    else 
    { 
     MessageBox.Show("No record found"); 
    } 
    con.Close(); 
} 
+0

ありがとうございました!それは:) – Miza

1

あなただけの更新を実行する必要があります。

private void button1_Click_1(object sender, EventArgs e) 
    { 
     con.Open(); 
     bool exists2; 
     using (SqlCommand cmd2 = new SqlCommand("select count(*) from [m_emp_photo] where [email protected]", con)) 
      { 
       cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text); 
       cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text); 
       exists2 = (int)cmd2.ExecuteScalar() > 0; 
      } 

     if (exists2) 
     { 
      string query2 = ""; 
      //query2 = "update m_emp_photo set [email protected] where [email protected]"; 

      query2 = "UPDATE m_emp_photo set [email protected] where [email protected]"; 

      SqlCommand cmd2; 
      cmd2 = new SqlCommand(query2, con); 

      cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text); 
      cmd2.ExecuteNonQuery(); 
      MessageBox.Show("Changes has been saved!"); 
     } 

     else 
     { 
      MessageBox.Show("No record found"); 
     } 
     con.Close(); 
    } 
0

私はあなたがcmd2.ExecuteNonQuery();

private void button1_Click_1(object sender, EventArgs e) 
     { 
      con.Open(); 
      bool exists2; 
      using (SqlCommand cmd2 = new SqlCommand("select count(*) from [m_emp_photo] where [email protected]", con)) 
       { 
        cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text);      
        exists2 = (int)cmd2.ExecuteScalar() > 0; 
       } 

      if (exists2) 
      { 
       string query2 = ""; 
       //query2 = "update m_emp_photo set [email protected] where [email protected]"; 

       query2 = "UPDATE m_emp_photo set [email protected] where [email protected]"; 

       SqlCommand cmd2; 
       cmd2 = new SqlCommand(query2, con); 

       cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text); 
       cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text); 

       cmd2.ExecuteNonQuery(); 
       MessageBox.Show("Changes has been saved!"); 
      } 

      else 
      { 
       MessageBox.Show("No record found"); 
      } 
      con.Close(); 
     } 
+0

技術的には 'ExecuteNonQuery'を使用するのはなぜですか? –

+0

はいSQLCommandが私の答えを更新しました –

0

コードにはいくつかの問題があります。

まず、更新を実行しませんでした。この行を追加します。

cmd2.ExecuteNonQuery(); 

第二に、あなたの最初のコマンドは@pathパラメータを持っていないので、なぜあなたはそれを追加していますか?

第3に、新しいコマンドを作成したため、パラメータが失われました。あなたは@pathでやったように、再び@empnoパラメータを追加する必要があります。

cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text); 

第四に、新しいコマンドを構築するので、あなたは別のusingのステートメントを必要としています。

private void button1_Click_1(object sender, EventArgs e) 
    { 
     con.Open(); 
     bool exists2; 
     using (SqlCommand cmd2 = new SqlCommand("select count(*) from [m_emp_photo] where [email protected]", con)) 
      { 
       cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text); 
       exists2 = (int)cmd2.ExecuteScalar() > 0; 
      } 

     if (exists2) 
     { 
      using (SqlCommand cmd2 = new SqlCommand("UPDATE m_emp_photo set [email protected] where [email protected]", con)) 
      { 
       cmd2.Parameters.AddWithValue("@empno", textBoxEmpNo.Text); 
       cmd2.Parameters.AddWithValue("@path", textBoxImgPath.Text); 
       cmd2.ExecuteNonQuery(); 
       MessageBox.Show("Changes has been saved!"); 
      } 
     } 
     else 
     { 
      MessageBox.Show("No record found"); 
     } 
     con.Close(); 
    } 

最後の一つです:

あなたのコードは次のようなものでなければなりません。ほとんどのデータベースでupdateステートメントが更新された行の数を返します。更新ステートメントが成功したかどうかはフィードバックとして使用できます。このような何か:

if(cmd2.ExecuteNonQuery() == 1) 
{ 
    MessageBox.Show("Changes has been saved!"); 
} 
else 
{ 
    MessageBox.Show("Failed to save changes!"); 
} 

はしかし、これでは十分ではありません、非常にtry...catch声明の中で、あなたのコード全体をラップでフレンドリーなエラーを表示することをお勧めします。

関連する問題