更新记录

时间:2011-06-28 07:34:37

标签: c# .net sql sql-server ado.net

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  ','

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:1)

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

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