如何更新 MS Access 数据库
我需要一个简单的程序来更新 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我以前从未尝试过使用 .NET
DataSet
访问 Access 数据库,但我认为您可以将 button1_Click 中的代码替换为如下内容:是的,我知道您会丢失一些
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:Yes, I know you'd be losing some of the benefits of the
DataSet
, but research suggests that the regularOleDbDataAdapter.Update
function doesn't play well with Access.