简体   繁体   English

如何从Access复制SQL Server数据库

[英]How to copy SQL Server database from Access

We have an Access UI to Sql Server database. 我们有一个到SQL Server数据库的Access UI。 The user connects to many databases (containing the same tables with different data), can choose between them. 用户连接到许多数据库(包含具有不同数据的相同表),可以在它们之间进行选择。 We use this for versioning. 我们使用它进行版本控制。 We want to make him able to copy and delete databases right from Access UI. 我们希望使他能够直接从Access UI复制和删除数据库。 He should be able to copy at least to the same server, and ideally also to other server. 他应该至少能够复制到同一台服务器,理想情况下还可以复制到其他服务器。

A Backup and restore is probably going to be your best bet. 备份和还原可能是您最好的选择。 There is another way as well. 还有另一种方法。 But there will be some restrictions. 但是会有一些限制。

You can detach the DB you want to copy, make a copy of the files attach the old one, and attach the new one as a new DB. 您可以分离要复制的数据库,在文件副本上附加旧文件,然后将新文件作为新数据库附加。 Your problem will be because you are using Access to connect to the DB, you will not be able to detach it because there is a connection to it, and all connections must be dropped before you can detach it. 您的问题将是因为您使用Access连接到数据库,由于与数据库有连接而无法分离它,并且必须先断开所有连接才能分离它。

Dropping the DB (delete it) will have the same problem. 删除数据库(删除它)将有同样的问题。 It won't drop unless you there is no connections on the DB. 除非您在数据库上没有任何连接,否则它不会丢失。

This is my final solution: 这是我的最终解决方案:

Dim conn As New ADODB.Connection

conn.ConnectionString = "Provider=SQLOLEDB;Data Source=" dbServer & ";" _
    & "User ID=" & user & ";Password=" & password
conn.Open

' backup
conn.Execute "BACKUP DATABASE [" & sourceDb & "] TO  [backup device] WITH NOFORMAT, NOINIT,  NAME = N'backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10"

' restore
conn.Execute "RESTORE DATABASE [" & targetDb & "] FROM  [abcosting temporary backup] WITH  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 10, " & mdf_move & ", " & ldf_move

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

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