Home > Ask the SQL Server Experts > Michael Hotek Questions & Answers > Error with RESTORE DATABASE in vb.net code
Ask The SQL Server Expert: Questions & Answers
EMAIL THIS

Error with RESTORE DATABASE in vb.net code

Michael Hotek EXPERT RESPONSE FROM: Michael Hotek

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


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


>
QUESTION POSED ON: 07 June 2004
I am trying to perform a RESTORE DATABASE operation, using T-SQL in a vb.net project on the current SQL database connection. Below are two code snippets I am working with:

1.

cn.open() 'current connection
Dim comSQL As New SqlClient.SqlCommand
comSQL.Connection = cn

comSQL.CommandText = "RESTORE DATABASE  FROM DISK = '' WITH REPLACE, NORECOVERY, PASSWORD = ''"

comSQL.ExecuteNonQuery()
cn.close()

2. Same as above, except I use the SQLDataAdapter, SQLDataReader to execute the RESTORE clause.

In both cases, the operation fails and the error message 'excusive access could not be obtained because the database is in use' is generated. The current database connection is only used as a placeholder for user databases that are brought into the application for data editing and reporting, so the current database info is disposable. How can I perform this restore operation in code?



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



RELATED CONTENT
SQL/Transact SQL (T-SQL)
Using DELETE and TRUNCATE TABLE statements to delete data in SQL Server
SQL language crash course (just enough to be dangerous)
Working with IntelliSense in SQL Server 2008 Management Studio
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Working with sparse columns in SQL Server 2008
Determining the source of full transaction logs in SQL Server
New GROUP BY option provides better data control in SQL Server 2008
Using the OPENROWSET function in SQL Server
Loading data files with SQL Server's BULK INSERT statement
Importing and exporting bulk data with SQL Server's bcp utility
SQL/Transact SQL (T-SQL) Research

Michael Hotek
Applying LDF file to MDF without turning it to .BAK first?
Procedure for finding last modified info?
Migrating poorly designed database to new, normalized one
Displaying decimal places in SQL Server 2000
Creating a DTS package
Extracting data without strings converted to strange characters
Money data type conversion issue using DTS
Any way to alias a call to functions?
Repairing corrupt MDF files
In-place versus side-by-side upgrade

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
ACID  (SearchSQLServer.com)
Collaboration Data Objects  (SearchSQLServer.com)
commit  (SearchSQLServer.com)
container  (SearchSQLServer.com)
DAO  (SearchSQLServer.com)
fetch  (SearchSQLServer.com)
OLE DB  (SearchSQLServer.com)
query  (SearchSQLServer.com)
SQL  (SearchSQLServer.com)
T-SQL  (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


Change your database context to master. You will also have to kill off any SPID that is connected to the database you are trying to restore. Refer to the kill command in BOL.

For More Information




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



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

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




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