2011-06-28 3 views
4
private void button1_Click(object sender, EventArgs e) 
{ 
    using (SqlConnection sqlConn = new SqlConnection("Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True")) 
    { 
     string sqlQuery = @"UPDATE cottonpurchase SET @slipNo, @basicprice, @weight, @totalamountbasic, @premium, @totalamountpremium, @totalamountpaid, @yeildestimates WHERE farmercode = @farmercode"; 
     { 
      SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn); 
      cmd.Parameters.Add("@slipNo", SqlDbType.Int).Value = TxtSlipNo.Text; 
      cmd.Parameters.Add("@basicprice", SqlDbType.Int).Value = TxtBasicPrice.Text; 
      cmd.Parameters.Add("@weight", SqlDbType.Int).Value = TxtWeight.Text; 
      cmd.Parameters.Add("@totalamountbasic", SqlDbType.Int).Value = TxtTotalAmountBasic.Text; 
      cmd.Parameters.Add("@premium", SqlDbType.Int).Value = TxtPremium.Text; 
      cmd.Parameters.Add("@totalamountpremium", SqlDbType.Int).Value = TxtTotalAmountPremium.Text; 
      cmd.Parameters.Add("@totalamountpaid", SqlDbType.Int).Value = TxtTotalAmountPaid.Text; 
      cmd.Parameters.Add("@yeildestimates", SqlDbType.Int).Value = TxtYeildEstimates.Text; 

      sqlConn.Open(); 
      try 
      { 
       cmd.ExecuteNonQuery(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
    } 
} 

Il me donne une erreur, même si tout semble bien avec mon code:Mise à jour des enregistrements

error : incorrect syntax near ',' 

Répondre

4

Vous devez spécifier les noms de colonne que vous essayez de définir.

string sqlQuery = @" 
    UPDATE cottonpurchase 
    SET 
     slipNo = @slipNo, 
     basicprice= @basicprice, 
     weight = @weight, 
     totalamountbasic = @totalamountbasic, 
     premium = @premium, 
     totalamountpremium = @totalamountpremium, 
     totalamountpaid = @totalamountpaid, 
     yeildestimates = @yeildestimates 
    WHERE farmercode = @farmercode"; 

En outre, vous n'avez pas fourni paramètre @farmercode:

cmd.Parameters.AddWithValue("@farmercode", <someValue>); 
+0

maintenant sa me donner l'erreur que je dois déclarer une farmercode variable mais je ne peux pas le faire parce que son cours tht varient – tanya

+1

@tanya - C'est parce que vous n'avez pas ajouté de paramètre @farmercode. –

+0

ahh merci ça a marché :) – tanya

1

Vous avez oublié de mentionner les noms de colonnes dans l'ensemble.

string sqlQuery = @"UPDATE cottonpurchase SET [email protected], [email protected], ... WHERE farmercode = @farmercode"; 
Questions connexes