查询以更新值

时间:2012-02-11 10:24:40

标签: c# visual-studio-2010 ms-access

我正在尝试更新我的数据库,即MS Access文件,我想通过从文本框中获取值来更新我的表,但我无法编写正确的查询。

有人可以帮我写一个正确的更新查询吗?

string strconn4 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|pay.accdb";

OleDbConnection sqlconn4 = new OleDbConnection(strconn4);
sqlconn4.Open();    
OleDbCommand ocmd = new OleDbCommand("UPDATE fees SET fname=" + Convert.ToString(textBox2.Text) + ",lname=" + Convert.ToString(textBox3.Text) + ",amtpayd=" + Convert.ToString(textBox4.Text) + ",amtleft=" + Convert.ToString(textBox5.Text) + ",disc=" + Convert.ToString(textBox6.Text) + ",pdate=" + Convert.ToString(dateTimePicker3.Text) + ",rdate=" + Convert.ToString(dateTimePicker1.Text) + ",WHERE memid=" + Convert.ToString(textBox1.Text), sqlconn4);

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

喜欢这个

   string sql=string.Format("UPDATE Table1 SET column1='{0}',column2='{1}' where id={2}",tbx1.text,tbx2.text,tbx3.text);
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = strconn4;
      SqlCommand cmd = new SqlCommand();
             cmd.CommandText = sql;
             cmd.Connection = conn;
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();