简体   繁体   English

C#将MS-Access数据库导入SQL Server?

[英]C# import MS-Access database into SQL Server?

I need to import data from a MS Access database into a SQl Server 2000 database once a day. 我需要每天一次将来自MS Access数据库的数据导入到SQl Server 2000数据库中。 Since I need this to be done every day automated is there ac# routine which would be able to do this? 由于我需要每天自动完成此操作,是否有ac#例程可以执行此操作?

The tables in the MS Access database will be named as the same tables in the SQL Server and it needs to delete the tables in the SQL Server database and then import the new data/tables from MS Access. MS Access数据库中的表将在SQL Server中被命名为相同的表,它需要删除SQL Server数据库中的表,然后从MS Access导入新的数据/表。

One option is to create a stored procedure in your sql server database to do it, see here: select from access database file and insert to sql Database 一种选择是在sql服务器数据库中创建一个存储过程来执行此操作,请参见此处: 从访问数据库文件中选择并插入到sql数据库中

As was mentioned by marc_s you may be able to use SSIS - but there could be quite a bit of learning involved in getting that right. 正如marc_s所提到的那样,您可能可以使用SSIS-但要想正确使用它可能需要大量的学习。

Another option is to control it from within C# - pulling data from the access DB and then inserting it into the sql server - this is unlikely to be what you want. 另一个选择是从C#中控制它-从访问数据库中提取数据,然后将其插入到sql服务器中-这不太可能是您想要的。

If your access db and the sql server instance are guaranteed to be on the same server then I would be inclined to try option 1. If not then it opens up dozens of additional questions ;) 如果保证您的访问数据库和sql服务器实例位于同一台服务器上,那么我将倾向于尝试选项1。如果没有,那么它将带来许多其他问题;)

EDIT: you mention it can be done by clicking a button - in this case really you should give the SSIS solution a once over at least and see if it looks like what you want, it is designed for exactly this type of scenario. 编辑:您提到可以通过单击按钮来完成-在这种情况下,实际上您应该至少给SSIS解决方案一次,看看它是否看起来像您想要的那样,它正是针对这种情况而设计的。

access.mdb linked servers in sql server any versions, it should be needed Microsoft.ACE.OLEDB.12.0 or Microsoft.Jet.OLEDB.4.0. access.mdb链接的服务器在sql server的任何版本中,都需要Microsoft.ACE.OLEDB.12.0或Microsoft.Jet.OLEDB.4.0。 those drivers are existed two types 32 bit and 64 bit. 这些驱动程序存在两种类型的32位和64位。 64 bit os prefer install 64 bit jet drivers other wise linking not may work. 64位操作系统更喜欢安装64位喷射驱动程序,否则可能无法正常连接。

Three steps: 三个步骤:

1 - Create a LinkedServer in SQLServer like this: 1-在SQLServer中创建LinkedServer,如下所示:
EXEC master.dbo.sp_addlinkedserver @server = 'Customers', @srvproduct = 'CustomersDB', @provider = 'Microsoft.Jet.OLEDB.4.0', @datasrc = 'C:\\inetpub\\wwwroot\\ftp\\AccessDB.MDB', @location = '', @provstr = '', @catalog = ''
EXEC master.dbo.sp_serveroption @server = 'Customers', @optname = 'collation compatible', @optvalue = 'false'

2 - Create a stored procedure inside your target sqlserver doing the import work 2-在执行导入工作的目标sqlserver中创建一个存储过程
CREATE PROCEDURE ImportMDBData
as
TRUNCATE TABLE sqlServerCustTable
INSERT INTO sqlServerCustTable (list your columns name here) (SELECT * FROM Customers.dbo.mdbCustTable)

3 - Now you just need to call the stored procedure from your code 3-现在您只需要从代码中调用存储过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM