Reputation: 471
i have my c# application, and i want to restore a database using my application . my sql code is like this:
alter database SSDPrototypeV3 set offline with rollback immediate
restore database SSDPrototypeV3
from disk = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Backup\dustinepogi.bak'
alter database SSDPrototypeV3 set online
when i run this on my application, it successfully restores my db . but when i try to run a sql query (like a select statement), it says that i have an invalid object name, specifically on the table that i performed my select statement . but then, if i reload my application after closing, it is fully restored . how can i get rid of this problem ?
Upvotes: 0
Views: 869
Reputation: 19161
Assuming you are using SqlClient
(and you are using connection pooling):
SqlConnection.ClearAllPools
(clears all pools for the provider) or ClearPool
(clears just the pool associated with the specific connection string)And, hopefully, off you go
Upvotes: 1
Reputation: 172270
A database connection has a current database that all queries are executed against. The initial database parameter of the connection string determines the current database used when the connection is opened.
I assume that the current database changes back to master
when you take your SSDPrototypeV3
database offline. You have two options to fix this:
use SSDPrototypeV3
orUpvotes: 0