如何使用 .NET 中的 OleDb 命名空间连接到 Access 数据库?
我想为我的 Windows 窗体应用程序使用 Access 数据库。 (用C#编写)
我已使用 OleDb 命名空间进行连接,并且可以使用 OleDbConnection
和 ExecuteReader
对象从源中选择记录。
但是,我还无法插入、更新或删除记录。
我的代码如下:
OleDbConnection con = new OleDbConnection(strCon);
try
{
string con="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xyz.mdb;Persist Security Info=True";
con.Open();
OleDbCommand com = new OleDbCommand("INSERT INTO DPMaster(DPID, DPName, ClientID, ClientName) VALUES('53', 'we', '41', 'aw')", con);
int a = com.ExecuteNonQuery();
//OleDbCommand com = new OleDbCommand("SELECT * FROM DPMaster", con);
//OleDbDataReader dr = com.ExecuteReader();
//while (dr.Read())
//{
// MessageBox.Show(dr[2].ToString());
//}
MessageBox.Show(a.ToString());
}
catch
{
MessageBox.Show("cannot");
}
如果执行注释块,则应用程序工作正常。但插入块却没有。
知道了这一点,为什么我无法插入、更新或删除数据库记录?
I want to use an Access database for my Windows Forms application. (written with C#)
I have used OleDb namespace for connecting, and I'm able to select the records from the source using the OleDbConnection
and ExecuteReader
objects.
However, I can't insert, update or delete records yet.
My code is the following:
OleDbConnection con = new OleDbConnection(strCon);
try
{
string con="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xyz.mdb;Persist Security Info=True";
con.Open();
OleDbCommand com = new OleDbCommand("INSERT INTO DPMaster(DPID, DPName, ClientID, ClientName) VALUES('53', 'we', '41', 'aw')", con);
int a = com.ExecuteNonQuery();
//OleDbCommand com = new OleDbCommand("SELECT * FROM DPMaster", con);
//OleDbDataReader dr = com.ExecuteReader();
//while (dr.Read())
//{
// MessageBox.Show(dr[2].ToString());
//}
MessageBox.Show(a.ToString());
}
catch
{
MessageBox.Show("cannot");
}
If I the commentted block is executed, the application works fine. But the insert block doesn't.
Knowing this, why I am unable to insert, update or delete database records?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己遇到的问题是:
您已将 mdb 文件添加到您的解决方案中,每次运行该程序时,它都会被复制到调试文件夹中。
因此,您可以从中进行选择,但删除行不会影响解决方案中的原始文件。
检查一下。
the problem that I encountered myself is as:
You've added the mdb file to your solution and every time you run the program it will be copied into debug folder.
So you can select from it but deleting rows doesn't affect the original file you have in your solution.
Check for it.
首先,永远不要扼杀你的例外。最好让你的异常冒出来,这样你就可以获得有关不正常工作的重要信息。最好这样写:
其次
,尽可能多地使用
using
块,因为这些块将处理不再需要的对象。因此,您的代码应该如下所示:使用此代码,您将更有可能在异常冒出时知道出了什么问题,此外,一旦您退出
using
块,当您的对象被处置时,所使用的资源将被释放。First, never strangle your exception. It is better to let your exception bubble up so that you may get important information regarding what is not working properly. It is better to write:
than
Second, make use of
using
blocks as much as possible, since those blocks will dispose the no longer needed objects. So your code should look like this:With this code, you will more likely get to know what is going wrong while the exception will bubble up, plus, as soon as you'll quit the scope of the
using
blocks, resources used will be freed up as your objects will get disposed.