如何使用 Visual C# 从 Access mdb 获取数据并将其放入 postgresql 表中?

发布于 2024-09-08 07:50:35 字数 259 浏览 1 评论 0原文

好吧,我已经做了 - 我能够通过 Microsoft.Jet.OLEDB 提供程序连接访问 mdb 表,然后我可以使用选择查询、OleDbDataAdapter 和 DataSet 从表中获取数据。

现在,我可以通过 Npgsql 连接到 Postgresql,但是我不明白 - 我如何从访问表中获取数据并将其放入 postgresql 表中?

我想要完成的是从 access mdb 表获取数据并使用“select into”查询将其插入到 postgresql 表中。

Well, what i've already done - is i'm able to connect to access mdb table via Microsoft.Jet.OLEDB provider, then i can get a data from table, using select query, OleDbDataAdapter and a DataSet.

Now, i'm able to connect to Postgresql via Npgsql, but what escapes me - is how am i able to get the data from access table and put it into postgresql table?

What i want to accomplish - is get a data from access mdb table and insert it into a postgresql table using "select into" query.

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

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

发布评论

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

评论(3

零崎曲识 2024-09-15 07:50:35

在这种情况下,我认为您无法使用从一个数据库到另一个数据库的“SELECT INTO”查询(因为这两个数据库位于不同的引擎上)。您需要做的是将读取的数据存储到 MDB 中的某个 CLR 对象中,然后将 CLR 数据读取到 PostgreSQL 数据库中,这是相当简单的。只是不可能按照你想要的方式去做

I don't think you'll be able to use a "SELECT INTO" query from one database to another in this instance (since the two databases are on different engines). What you will have to do is store read the data into some CLR object from the MDB and then read the CLR data into the PostgreSQL database, which is fairly trivial. It's just not possible to do it the way you want

三五鸿雁 2024-09-15 07:50:35

这里有一些非常业余的笔记,可能会对您有所帮助。他们指的是 SQL Server Express,因为我无权访问 postgressql。我通过将 SQL Server 表链接到 Access 并检查连接属性获得了连接字符串,但是,您也可以在此处查看: http://www.connectionstrings.com/postgre-sql。这两行将 Access 中表 1 中的值插入 SQL Express 中表 2 中。在 Access 中,如果查询不正确,查询将失败且不会发出警告(在本例中为 Access 2010)。我在 Visual c# 2010 Express 中尝试过此操作。

//create the database connection
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\docs\\db.mdb");

//create the command object and store the sql query
OleDbCommand aCommand = new OleDbCommand("INSERT into [ODBC;Description=TEST;DRIVER=SQL Server;SERVER=COMP\\SQLEXPRESS;Trusted_Connection=Yes;DATABASE=test].Table2 (id, atext) select id, atext from table1", aConnection);

Here are some very amateur notes that may help you. They refer to SQL Server Express, because I do not have access to postgressql. I got the connection string from linking the SQL server table to Access and checking the connection property, however, you can also look here: http://www.connectionstrings.com/postgre-sql. The two lines insert values from table1 in Access into table2 in SQL Express. In Access, the query will fail without warnings if it is not correct (Access 2010 in this case). I tried this in Visual c# 2010 Express.

//create the database connection
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\docs\\db.mdb");

//create the command object and store the sql query
OleDbCommand aCommand = new OleDbCommand("INSERT into [ODBC;Description=TEST;DRIVER=SQL Server;SERVER=COMP\\SQLEXPRESS;Trusted_Connection=Yes;DATABASE=test].Table2 (id, atext) select id, atext from table1", aConnection);
花海 2024-09-15 07:50:35

嘿嘿,感谢所有帮助过我的人,我终于明白了。
因此,对于所有遇到相同情况的人 - 这是具有正确的 Postgresql 连接字符串的解决方案:

//create the database connection
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\docs\\db.mdb");

//create the command object and store the sql query
OleDbCommand aCommand = new OleDbCommand("INSERT into [ODBC;DSN=PostgreSQL;DATABASE=postgres;SERVER=localhost;PORT=5432;UID=postgres;PWD=test;].Table2 (id, atext) select id, atext from table1", aConnection);

Hey, thanks to all who have helped me, i've finally figured it out.
So, for all who will encounter the same situation - here's the solution with a correct connection string for Postgresql.:

//create the database connection
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\docs\\db.mdb");

//create the command object and store the sql query
OleDbCommand aCommand = new OleDbCommand("INSERT into [ODBC;DSN=PostgreSQL;DATABASE=postgres;SERVER=localhost;PORT=5432;UID=postgres;PWD=test;].Table2 (id, atext) select id, atext from table1", aConnection);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文