正在更新记录

本文关键字:新记录 更新 | 更新日期: 2023-09-27 18:00:37

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

您忘记提及集合中的列名。

string sqlQuery = @"UPDATE cottonpurchase SET slipNo=@slipNo, basicprice=@basicprice, ...  WHERE farmercode = @farmercode";