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

即使我的代码一切正常,它也给了我一个错误:
error : incorrect syntax near  ','

最佳答案

您需要指定要设置的列名称。

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

此外,您没有提供 @farmercode 参数:
cmd.Parameters.AddWithValue("@farmercode", <someValue>);

关于c# - 更新记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6503114/

10-11 17:24