将值插入或更新为空文本框

时间:2011-06-22 17:15:50

标签: c# sql winforms

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

这就是我现在所做的,但没有任何反应!我希望能够更新空值但没有任何反应!请帮忙

2 个答案:

答案 0 :(得分:1)

此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         
} 
......  

答案 1 :(得分:0)

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