Home > Ask the SQL Server Experts > Roman Rehak - SQL Server Development and Reporting Services Questions & Answers > Code to restore SQL Server databases in VB.NET
Ask The SQL Server Expert: Questions & Answers
EMAIL THIS

Code to restore SQL Server databases in VB.NET

Roman Rehak EXPERT RESPONSE FROM: Roman Rehak

Pose a Question
Other SQL Server Categories
Meet all SQL Server Experts
Become an Expert for this site


Expert advice on database development
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 20 February 2008
I am unable to restore a SQL Server database using Visual Basic.Net (VB.Net) code. Could you please provide a code for restoring databases in VB.Net?

>
EXPERT RESPONSE
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
Catalog=master;Integrated Security=SSPI;")
cn.Open()
Dim cmd As SqlCommand = New SqlCommand(sql, cn)
cmd.ExecuteNonQuery()

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.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
.NET development for SQL Server
Custom VB.Net scripting in SQL Server Integration Services
Connect to SQL Server database with Visual Basics
SQL Server Blog Watch
Top 10 SQL Server development questions
Developing CLR database objects: 10 tips, 10 minutes
CLR architecture
CLR assemblies in SQL Server 2005
CLR stored procedures
CLR triggers
CLR aggregates
.NET development for SQL Server Research

SQL Server backup and recovery
Tips for scheduling and testing SQL Server backups
Tricking SQL Server into making full database backups
Top 7 SQL Server backup and restore tips of 2007
Retrieve deleted tables in SQL Server
SQL Server backup and restore commands to limit downtime
Mirrored backup and restore commands in SQL Server 2005
Set up a SQL Server disaster recovery site
Best practices for SQL Server backup maintenance
Synchronize databases on two separate active servers
Back up tables from SQL Server 2005 database to a file
SQL Server backup and recovery Research

Roman Rehak - SQL Server Development and Reporting Services
Using BULK INSERT to insert rows from SQL Server dataset to table
SQL Server query to import database names
Troubleshoot SQL Server 2005 SP2 installation error
Workaround to Reporting Services error in SQL Server Express
How can I export stored procedures in SQL Server 2005?
Stored procedure for a BULK INSERT to SQL Server 2005 text file
BULK INSERT to import data from file to SQL Server database
INSTEAD OF trigger to update a SQL Server table
Retrieve images from SQL Server and store in VB.Net
Can you import Excel data to SQL Server Reporting Services?

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
rollback  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsWebcastsWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2005 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts