如何更新 MS Access 数据库

发布于 2024-10-26 20:08:58 字数 2153 浏览 1 评论 0原文

我需要一个简单的程序来更新 MS Access 数据库字段。我遵循了在线教程,该教程很简单并且代码可以运行。但当我重新实现它时,它似乎不再起作用了。这是我的代码。

 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 更新字段 row[0] (第一行)和 column[3] (第四列)。我得到的错误是 Synatx 错误FROM 子句。经过一些互联网阅读后,我发现 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");
    }
}

当我这样做时,我收到一个新错误,UPDATE 语句中的语法错误。我在网上读了很多书,但似乎没有一个能解决这个问题。他们都以不同的方式使用Update命令。我只知道这个方法。我的代码有什么问题?特别是因为这以前有效。或者说这不是更新正确技术的方式吗?请帮助我编写代码,而不是我不遵循的技术术语。或者有其他程序可以在 ms access 中更新吗?

谢谢。

I need a simple program to update MS Access database fields. I followed an online tutorial which was simple and had the code working. But it doesnt seem to work anymore when I reimplement it. Here's my code.

 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 is the table name of my database. What I tried to do is update the field row[0] (first row) and column[3] (4th column) with the string hello.. The error i get is Synatx error in FROM clause. After some internet reading, I found user has to be in square brackets. So I made it this.

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

When I do this, I get a new error, Syntax error in UPDATE statement. I did a lot of internet reading but none seems to address this. They all have used Update command differently. I know only this way. What's wrong in my code? Especially since this worked before. Or isn't this way of updating a proper technique? Please help me with the code and not technical terms which I don't follow. Or any other procedure to update in ms access?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-11-02 20:08:58

我以前从未尝试过使用 .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 配合使用。

I've never tried to access an Access database with a .NET DataSet before, but I think you could replace the code in button1_Click with something like this:

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

Yes, I know you'd be losing some of the benefits of the DataSet, but research suggests that the regular OleDbDataAdapter.Update function doesn't play well with Access.

最近可好 2024-11-02 20:08:58
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*
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*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文