将值插入或更新到空文本框中

本文关键字:文本 更新 插入 | 更新日期: 2023-09-27 17:59:54

    public void Updatecottonpurchase(int slipno, int basicprice, int premium, int totalamountpaid, int weight, int totalamountbasic, int totalamountpremium, int yeildestimates, int farmercode)
    {
        SqlConnection sqlConn = new SqlConnection(@"Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True");
        try
        {
            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);
            sqlConn.Open();
            cmd.ExecuteNonQuery();
            sqlConn.Close();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            sqlConn.Close();
        }
        finally
        {
            sqlConn.Close();
        }
    } 

这就是我现在所做的,但什么也没发生!我想更新空值,但什么都没发生!请帮助

将值插入或更新到空文本框中

此SQL代码:

UPDATE TABLE cottonpurchase SET  slipno= WHERE farmercode=

不执行任何操作,您需要添加参数,
请参阅:http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx

您需要将代码更改为:

....
string queryString = 
     "UPDATE TABLE cottonpurchase SET slipno=@slipno WHERE farmercode=@farmercode"; 
try       
{          
  connection.Open();          
  SqlCommand command = new SqlCommand(queryString, connection);
  //define parameters used in command object
  SqlParameter param  = new SqlParameter();
  param.ParameterName = "@slipno";
  param.Value         = inputfromsomewhere;
  SqlParameter param  = new SqlParameter();
  param.ParameterName = "@farmercode";
  param.Value         = inputfromsomewhereelse;
  //add new parameter to command object
  command.Parameters.Add(param);

  int result = command.ExecuteNonQuery();          
  //if result = 1 the update is performed         
} 
......  

您需要添加或选择一列用作主键。主键应唯一标识一行,并用于定位要更新的行。