I am unable to restore a SQL Server database using Visual Basic.Net (VB.Net) code. Could you please provide a code...
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
for restoring databases in VB.Net?
It's hard to tell exactly what the problem is without seeing your code. Executing RESTORE from VB.Net is not much different than executing from SQL Server. You need to construct a SQL Server statement and execute it with ADO.NET. Here is a sample for restoring the Northwind database:
Dim sql As String = "RESTORE DATABASE [Northwind] " & _
"FROM DISK = N'C:SQL Server DatabasesNorthwind.bak' " & _
"WITH FILE = 1," & _
"Move N'Northwind' TO N'C:SQLNorthwind.mdf'," & _
"Move N'Northwind_log' TO N'C:SQLNorthwind_1.ldf'," & _
"NOUNLOAD, STATS = 10"
Dim cn As SqlConnection = New SqlConnection("Data Source=rrehak;Initial
Dim cmd As SqlCommand = New SqlCommand(sql, cn)
Make sure the account you use in the connection string has sufficient privileges for the RESTORE command. If the database does not exist, the user needs CREATE DATABASE privileges. If the database already exists, the user has to be a member of the sysadmin or dbcreator server roles, or be the owner (dbo) of the database.
Dig Deeper on .NET Development for SQL Server
Related Q&A from Roman Rehak
There are a few things you can do to tune SQL Server for improved reporting performance. Here are some generic and Reporting Services-specific ...continue reading
Learn about working with image files in SQL Server, including advice on using queries to locate them, in this expert answer.continue reading
Discover the best solutions for encrypting, decrypting and restoring a database in SQL Server 2005.continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.