如何使用 .NET 中的 OleDb 命名空间连接到 Access 数据库?

发布于 2024-10-08 22:12:58 字数 939 浏览 3 评论 0原文

我想为我的 Windows 窗体应用程序使用 Access 数据库。 (用C#编写)
我已使用 OleDb 命名空间进行连接,并且可以使用 OleDbConnectionExecuteReader 对象从源中选择记录。
但是,我还无法插入、更新或删除记录。

我的代码如下:

 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 技术交流群。

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

发布评论

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

评论(2

梦冥 2024-10-15 22:12:58

我自己遇到的问题是:

您已将 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.

浅笑依然 2024-10-15 22:12:58

首先,永远不要扼杀你的例外。最好让你的异常冒出来,这样你就可以获得有关不正常工作的重要信息。最好这样写:

con.Open();
OleDbCommand com = new OleDbCommand("INSERT INTO DPMaster(DPID,DPName,ClientID,ClientName) VALUES('53','we','41','aw')", con);

int a = com.ExecuteNonQuery();

其次

try {
    con.Open();
    OleDbCommand com = new OleDbCommand("INSERT INTO DPMaster(DPID,DPName,ClientID,ClientName) VALUES('53','we','41','aw')", con);

    int a=com.ExecuteNonQuery();

} catch {
    MessageBox.Show("cannot");
}

,尽可能多地使用 using 块,因为这些块将处理不再需要的对象。因此,您的代码应该如下所示:

using (OleDbConnection con = new OleDbConnection(conStr))
    using (OleDbCommand com = new OleDbCommand("INSERT INTO DPMaster(DPID,DPName,ClientID,ClientName) VALUES('53','we','41','aw')", con) {

        con.Open();

        int a = com.ExecuteNonQuery();

        MessageBox.Show(a.ToString());
    }

使用此代码,您将更有可能在异常冒出时知道出了什么问题,此外,一旦您退出 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:

con.Open();
OleDbCommand com = new OleDbCommand("INSERT INTO DPMaster(DPID,DPName,ClientID,ClientName) VALUES('53','we','41','aw')", con);

int a = com.ExecuteNonQuery();

than

try {
    con.Open();
    OleDbCommand com = new OleDbCommand("INSERT INTO DPMaster(DPID,DPName,ClientID,ClientName) VALUES('53','we','41','aw')", con);

    int a=com.ExecuteNonQuery();

} catch {
    MessageBox.Show("cannot");
}

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:

using (OleDbConnection con = new OleDbConnection(conStr))
    using (OleDbCommand com = new OleDbCommand("INSERT INTO DPMaster(DPID,DPName,ClientID,ClientName) VALUES('53','we','41','aw')", con) {

        con.Open();

        int a = com.ExecuteNonQuery();

        MessageBox.Show(a.ToString());
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文