在c#中使用oledb更新数据

时间:2015-04-09 09:27:31

标签: c# ms-access oledb

我使用c#和数据库创建了一个项目,使用访问accdb并将它们连接起来。我做了2个按钮,第一个添加新的客户,工作得很好,第二个更新客户的数据(名字和姓氏),由于某种原因,更新按钮不起作用,没有错误时我运行该项目,但在点击后没有任何反应......

private void button2_Click(object sender, EventArgs e)
{
    connect.Open();
    string cid = textBox1.Text;
    string cfname = textBox2.Text;
    string clname = textBox3.Text;
    OleDbCommand command = new OleDbCommand();
    command.Connection = connect;
    command.CommandText = "UPDATE Tcostumers SET cfname= " + cfname + "clname= " + clname + " WHERE cid = " + cid;
    if (connect.State == ConnectionState.Open)
    {
        try
        {
            command.ExecuteNonQuery();
            MessageBox.Show("DATA UPDATED");
            connect.Close();
        }
        catch (Exception expe)
        {
            MessageBox.Show(expe.Source);
            connect.Close();
        }
    }
    else
    {
        MessageBox.Show("ERROR");
    }
}

3 个答案:

答案 0 :(得分:3)

我相信你的命令文本就是问题所在;

command.CommandText = "UPDATE Tcostumers SET cfname= " + cfname + "clname= " + clname + " WHERE cid = " + cid;

你需要在set语句之间使用逗号,并且Gino也指出了语音标记。

编辑:

比使用变量的参数更好,你当前的方法对SQL注入是开放的,例如。

    private void button2_Click(object sender, EventArgs e)
    {
        OleDbCommand command = new OleDbCommand(@"UPDATE Tcostumers
                                                    SET cfname = @CFName,
                                                        clname = @CLName
                                                    WHERE cid = @CID", connect);

        command.Parameters.AddWithValue("@CFName", textBox2.Text);
        command.Parameters.AddWithValue("@CLName", textBox3.Text);
        command.Parameters.AddWithValue("@CID", textBox1.Text);

        try
        {
            connect.Open();
        }
        catch (Exception expe)
        {
            MessageBox.Show(expe.Source);
        }
        try
        {
            command.ExecuteNonQuery();

            MessageBox.Show("DATA UPDATED");
        }
        catch (Exception expe)
        {
            MessageBox.Show(expe.Source);
        }
        finally
        {
            connect.Close();
        }
    }

我倾向于如何格式化我的代码,所以就像你一样。希望它有所帮助。

答案 1 :(得分:2)

这可能是一件愚蠢的事,但...... 您正在更新字符串而不是整数,因此请尝试添加''在你的字符串之类的东西:

command.CommandText = "UPDATE Tcostumers SET cfname= '" + cfname + "' clname='" + clname + "' WHERE cid = " + cid;

答案 2 :(得分:0)

//my sample code for edit/update 
Table Name = StudentFIle
Fields = id,fname,lname

bool found = false;
OleDbConnection BOMHConnection = new OleDbConnection(connect);
string sql = "SELECT * FROM StudentFIle";
BOMHConnection.Open();
OleDbCommand mrNoCommand = new OleDbCommand(sql, BOMHConnection);
OleDbDataReader mrNoReader = mrNoCommand.ExecuteReader();
while (mrNoReader.Read())
{
    if (mrNoReader["id"].ToString().ToUpper().Trim() == idtextbox.Text.Trim())
    {
        mrNoReader.Close();
        string query = "UPDATE StudentFIle set fname='" +firstnametextbox.Text+ "',lname='"+lastnametextbox.Text+"' where id="+idtextbox.Text+" ";
        mrNoCommand.CommandText = query;
        mrNoCommand.ExecuteNonQuery();
        MessageBox.Show("Successfully Updated");

        found = true;
        break;
    }
    continue;
}
if (found == false)
{
    MessageBox.Show("Id Doesn't Exist !.. ");
    mrNoReader.Close();
    BOMHConnection.Close();
    idtextbox.Focus();
}