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?
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
- Dozens more answers to tough SQL Server questions from Michael Hotek are available here.
- The Best Microsoft SQL Server Web Links: tips, tutorials, scripts, and more.
- Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL Server questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, SQL Server, DB2, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
Dig Deeper on SQL-Transact SQL (T-SQL)
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.