Copy table from one database to another in Windows Forms using C# and MS Access Oledb

余生长醉 提交于 2019-12-24 16:19:49

问题


How to copy a table from one database to another , I'm developing a windows app using c# in .NET.The copying has to be done by the app. Extract data into an empty table in database 2 from a filled table in database1.I'm using access db , Oledbconnection. I found some answers for sql server though , but not really helping.


回答1:


You can refer to the second DB in SQL and execute against a connection to the first mdb/accdb:

Connection

using System.Data.OleDb;
<...>
string ConnString =
     @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Z:\Docs\first.accdb";
OleDbConnection conn = new OleDbConnection(ConnString);

SQL

INSERT INTO Contacts ( ID, [A Text] ) IN 'z:\docs\New.accdb'
SELECT Contacts.ID, Contacts.[A Text]
FROM Contacts;

Or

INSERT INTO [;DATABASE=Z:\Docs\new.accdb].Contacts ( ID, [A Text] )
SELECT Contacts.ID, Contacts.[A Text]
FROM Contacts;

Or to create a table:

SELECT Contacts.ID, Contacts.[A Text] INTO Contacts IN 'z:\docs\New.accdb'
FROM Contacts;


来源:https://stackoverflow.com/questions/11740448/copy-table-from-one-database-to-another-in-windows-forms-using-c-sharp-and-ms-ac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!