public void ModificaNomeUtente(int slipno , int basicprice , int premium, int totalamountpaid, int weight , int totalamountbasic , int totalamountpremium , int yeildestimates , int farmercode)
{
// Creo la stringa di Connessione al DataBase
SqlConnection sqlConn = new SqlConnection(@"Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True");
// Prova
try
{
// Creo la query che andrà a modificare il nome utente
string sqlQuery = "UPDATE cottonpurchse SET slipno = '" + slipno + "' + basic price = '" + basicprice + "' + premium = '" + premium + "' + totalamountpaid = '" + totalamountpaid + "' + weight = '" + weight + "' + totalamountbasic = '" + totalamountbasic + "' + totalamountpremium = '" + totalamountpremium + "' + yeildestimated = '" + yeildestimates + "' = WHERE farmercode = '" + FarmerCode + "'";
SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
// Apro la connessione con il DataBase Login.sdf
sqlConn.Open();
// Eseguo l'istruzione sql
cmd.ExecuteNonQuery();
// Chiudo la connessione con il DataBase Login.sdf
sqlConn.Close();
// Se la query ha avuto esito positivo imposto la variabile result a true
result = true;
}
// In caso di eccezzione
catch (Exception ex)
{
// Visualizzo il messaggio con la relativa eccezzione verificatasi
MessageBox.Show(ex.Message);
// Chiudo la connessione con il DataBase Login.sdf
sqlConn.Close();
// la query ha avuto esito negativo imposto la variabile result a false
result = false;
}
finally
{
// Chiudo la connessione con il DataBase Login.sdf
sqlConn.Close();
}
}
这不起作用它不更新可能是因为我的值被设置为null?
答案 0 :(得分:1)
该陈述解析为类似的东西;
UPDATE cottonpurchse
SET slipno = '???' + basic price = '???' + premium = '???' + totalamountpaid = '???' + weight = '???' + totalamountbasic = '???' + totalamountpremium = '???' + yeildestimated = '???' = WHERE farmercode = '???'
=
之前有WHERE
这是非法的,您的意思是,
而非+ ?
UPDATE cottonpurchse
SET slipno = '???' , basic price = '???' , premium = '???' , totalamountpaid = '???' , weight = '???' , totalamountbasic = '???' , totalamountpremium = '???' , yeildestimated = '???' WHERE farmercode = '???'
答案 1 :(得分:0)
更新:每个人的评论都有效。你的SQL充满了错误:额外的空格,额外的+和额外的=。你是在混淆自己试图建立一个字符串......
那就是说,你的SQL查询字符串应该是这样的:
string sqlQuery = "UPDATE cottonpurchse SET slipno=" + slipno + ", basicprice=" + basicprice + ", premium=" + premium + ", totalamountpaid=" + totalamountpaid + ", weight=" + weight + ", totalamountbasic=" + totalamountbasic + ", totalamountpremium=" + totalamountpremium + ", yeildestimated=" + yeildestimates + " WHERE farmercode=" + FarmerCode;
这是令人困惑的,因为你正在建立一个字符串。执行您尝试执行的操作的最佳(也是最安全)方法是使用SQL参数。请参阅:c# Sqlcommand error(特别是显示“Parameters.Add”的答案)