简体   繁体   English

使用 VB 脚本恢复 SQL .bak 文件?

[英]Restore a SQL .bak file using a VB script?

I am exploring methods to enable users to restore a database from within the project.我正在探索使用户能够从项目中恢复数据库的方法。

I have a project which I want to publish together with the database, so that users with no experience can use the program I have developed.我有一个项目,我想和数据库一起发布,以便没有经验的用户可以使用我开发的程序。

The code is written in VB script and I am able to publish the VB code, but am finding it difficult to include the database in the ClickOnce method.代码是用 VB 脚本编写的,我可以发布 VB 代码,但我发现很难将数据库包含在 ClickOnce 方法中。

I have been thinking that I could possibly copy the .bak file to the client's PC and then run a VB script that restores the database to the SQLserver that is installed in the ClickOnce install.我一直在想,我可以将 .bak 文件复制到客户端的 PC,然后运行 ​​VB 脚本,将数据库恢复到安装在 ClickOnce 安装中的 SQLserver。

Has anyone ever done this?有没有人做过这个?

I did find on the web something that looked promising, but it has been written for .NET framework 2.0 or 3.0 and I found that when I added System.Data.SqlClient 4.8.0 I got the following error:我确实在网上找到了一些看起来很有希望的东西,但它是为 .NET 框架 2.0 或 3.0 编写的,我发现当我添加System.Data.SqlClient 4.8.0 ,出现以下错误:

Severity Code Description Project File Line Suppression State Error Could not install package 'System.Data.SqlClient 4.8.0'.严重性代码描述项目文件行抑制状态错误无法安装包“System.Data.SqlClient 4.8.0”。 You are trying to install this package into a project that targets '.NETFramework,Version=v2.0', but the package does not contain any assembly references or content files that are compatible with that framework.您正在尝试将此包安装到以“.NETFramework,Version=v2.0”为目标的项目中,但该包不包含任何与该框架兼容的程序集引用或内容文件。 For more information, contact the package author.有关更多信息,请联系软件包作者。

The website where I got the code: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1433&lngWId=10我得到代码的网站: http : //www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1433&lngWId=10

Please can anyone help?请问有人可以帮忙吗?

VBScript, which would get run via wscript.exe is decades old technology通过 wscript.exe 运行的 VBScript 是几十年前的技术

VB.NET, which would get compiled into a custom .exe or .dll, is 2005+ (not included in this is .net core which is the past few years but I've not touched it) VB.NET 会被编译成自定义 .exe 或 .dll,是 2005+(不包括在过去几年中的 .net 核心,但我没有接触过它)

dim connectionString as string = "Provider=MSOLEDBSQL;Server=localhost;Database=myDataBase;Trusted_Connection=yes;"
dim connection as New OleDbConnection(connectionString)
Dim cmd As New OleDbCommand
Dim query As String = "RESTORE DATABASE AdventureWorks2012 FROM AdventureWorksBackups;"

Try
    cmd.CommandText = query
    cmd.ExecuteNonQuery()
Catch ex As Exception
    MsgBox("something bad happened")

End Try

Reference bits参考位

ExecuteNonQuery https://docs.microsoft.com/en-us/dotnet/api/system.data.oledb.oledbcommand.executenonquery?view=netframework-4.8 ExecuteNonQuery https://docs.microsoft.com/en-us/dotnet/api/system.data.oledb.oledbcommand.executenonquery?view=netframework-4.8

Restore database https://docs.microsoft.com/en-us/sql/t-sql/statements/restore-statements-transact-sql?view=sql-server-ver15恢复数据库https://docs.microsoft.com/en-us/sql/t-sql/statements/restore-statements-transact-sql?view=sql-server-ver15

and if you're not using OLEDB, or need a different provider, then futz with connection string as needed but that's the general approach如果您不使用 OLEDB,或者需要不同的提供程序,则根据需要使用连接字符串进行 futz,但这是一般方法

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

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