2017-06-20 5 views
2

Je sais que cette question a déjà été posée mais je ne comprends toujours pas pourquoi mon code n'est pas mis à jour dans la base de données. Ce code n'a pas d'erreur mais il ne change pas les données dans la base de données. Quelqu'un s'il vous plaît aidez-moi. Voici mon code:Comment mettre à jour dans la base de données

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(); 
    } 

Répondre

0

Le premier cmd2 n'a pas besoin d'ajouter le paramètre @path. Le deuxième cmd2 vous devez ajouter @empno paramètre et exécuter ExecuteNonQuery. J'espère que cela fonctionnera pour vous.

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

Merci pour votre aide! ça marche :) – Miza

1

Vous avez juste besoin d'exécuter la mise à jour:

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

je suppose que vous avez manqué d'exécuter votre SQLCommand dans Si la déclaration utilisation 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

techniquement ce n'est pas une requête, c'est pourquoi vous utilisez 'ExecuteNonQuery' ;-) –

+0

oui sa SQLCommand mis à jour ma réponse –

0

Il y a plusieurs problèmes dans votre code.

D'abord, vous n'avez pas exécuté la mise à jour. Ajoutez cette ligne:

cmd2.ExecuteNonQuery(); 

Deuxièmement, votre première commande paramètre ne pas @path, alors pourquoi ajouter-vous? Troisièmement, vous avez construit une nouvelle commande, vous avez donc perdu les paramètres. Vous devez ajouter le paramètre @empno à nouveau comme vous l'avez fait avec le @path:

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

Quatrièmement, depuis que vous avez construit une nouvelle commande, vous avez besoin une autre déclaration using.

Votre code devrait être quelque chose comme ceci:

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(); 
    } 

Une dernière chose. Je ne sais pas quelle base de données vous utilisez, mais l'instruction update dans presque toutes les bases de données renvoie le nombre de lignes mises à jour. Vous pouvez l'utiliser comme un retour si votre déclaration de mise à jour a réussi ou non. Quelque chose comme ceci:

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

Cependant, cela ne suffit pas, et il est fortement recommandé d'envelopper votre code entier dans une déclaration try...catch et afficher les erreurs amicales.