如何更新MS Access数据库

时间:2011-03-25 17:34:50

标签: c# syntax-error ms-access-2003

我需要一个简单的程序来更新MS Access数据库字段。我跟着online tutorial这很简单并且代码正常工作。但是当我重新实现它时它似乎不再起作用了。这是我的代码。

 public partial class Form1 : Form
 {
    public Form1()
    {
        InitializeComponent();
    }

    OleDbConnection conn;
    OleDbDataAdapter da;
    DataSet ds;
    OleDbCommandBuilder cb;
    DataRow row;

    private void Form1_Load(object sender, EventArgs e)
    {
        conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source = C:\\test.mdb");
        da = new OleDbDataAdapter("select* from user", conn);
        ds = new DataSet();

        conn.Open();
        da.Fill(ds, "user");
        conn.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        cb = new OleDbCommandBuilder(da);
        row = ds.Tables["user"].Rows[0];

        row[3] = "hello";

        da.Update(ds, "user");
    }
}

user是我的数据库的表名。我尝试做的是使用字符串hello更新字段行[0](第一行)和列[3](第4列)。我得到的错误是Synatx error in FROM clause。在一些网络阅读后,我发现user必须放在方括号内。所以我就这样做了。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    OleDbConnection conn;
    OleDbDataAdapter da;
    DataSet ds;
    OleDbCommandBuilder cb;
    DataRow row;

    private void Form1_Load(object sender, EventArgs e)
    {
        conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source = C:\\test.mdb");
        da = new OleDbDataAdapter("select* from [user]", conn);
        ds = new DataSet();

        conn.Open();
        da.Fill(ds, "user");
        conn.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        cb = new OleDbCommandBuilder(da);
        row = ds.Tables["user"].Rows[0];

        row[3] = "hello";

        da.Update(ds, "user");
    }
}

当我这样做时,我收到一个新错误Syntax error in UPDATE statement。我做了很多网络阅读,但似乎没有解决这个问题。他们都以不同的方式使用Update命令。我只知道这种方式。我的代码有什么问题?特别是因为这之前工作过。或者这不是更新正确技术的方法吗?请帮我处理代码,而不是我没有遵循的技术术语。或者以ms访问权限更新的任何其他程序?

感谢。

2 个答案:

答案 0 :(得分:4)

我之前从未尝试使用.NET DataSet访问Access数据库,但我认为您可以使用以下内容替换button1_Click中的代码:

private void button1_Click(object sender, EventArgs e)
{
   conn.Open();

   string query = "UPDATE [user] SET [columnname] = ? WHERE id = ?";
   var accessUpdateCommand = new OleDbCommand(query, conn);
   accessUpdateCommand.Parameters.AddWithValue("columnname", "hello");
   accessUpdateCommand.Parameters.AddWithValue("id", 123); // Replace "123" with the variable where your ID is stored. Maybe row[0] ?
   da.UpdateCommand = accessUpdateCommand;
   da.UpdateCommand.ExecuteNonQuery();

   conn.Close();
}

是的,我知道你会失去DataSet的一些好处,但是研究表明常规OleDbDataAdapter.Update功能与Access不兼容。

答案 1 :(得分:0)

student = txtStudent.Text;
            age = txtAge.Text;
            address = txtAddress.Text;
            section = CBSection.Text;



            string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\STI.accdb";
            string queryString = "UPDATE Details set StudentName='"+student+"',Age='"+age+"',Address='"+address+"' where Section/Course="+section+"";
            OleDbConnection connection = new OleDbConnection(ConnectionString);
            OleDbCommand command = new OleDbCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = queryString;
            command.Connection = connection;
            connection.Open();
            {
                try
                {
                    command.ExecuteNonQuery();
                    MessageBox.Show("StudentName : " + student + "\nAge : " + age + "\nAddress : " + address + "\nSection : " + section + "\nHas been successfully Enrolled");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }*strong text*