Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends,

i'm using SQL 2005 server database. I hv developed an application in a vb.net 10. i am taking backup. and it is working ver fine. bt while restoring it is giving error.
My code is :

Dim query As String
Try
query = "Restore Database Employee From Disk='H:\Stuff\Backup\backup1.bak' "
Dim con As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Trial1;Integrated Security=True;Pooling=False")
con.Open()
Dim cmd As SqlCommand = New SqlCommand(query, con)
cmd.ExecuteNonQuery()
MsgBox("Okk")
Catch ex As Exception
MsgBox(ex.ToString)
End Try

What is the mistake? please help me out.
Posted
Comments
[no name] 6-Jul-13 9:40am    
Could you tell what kind of error is it?
BCD23 6-Jul-13 9:42am    
yaa... It is throwing exception as

RESTORE cannot process database 'Trial1' because it is in use by this session. It is recommended that the master database be used when performing this operation.
RESTORE DATABASE is terminating abnormally.
[no name] 6-Jul-13 9:45am    
Pretty obvious is it not? You have to have all of your users disconnected before your can restore a database.
BCD23 6-Jul-13 9:47am    
bt for that, what shoukd i do? close a connection? throwing exception is at executeNonQuery()

1 solution

The error message is pretty explicit: your database is in use. You have to close all connections to the DB while you do the restore. Not just your current app - but every application that is accessing the database has to be shut down, on all PCs.
 
Share this answer
 
Comments
BCD23 6-Jul-13 9:56am    
okk. bt der is no other connection except my application connection.
BCD23 6-Jul-13 9:57am    
i'm not getting how to disconnect database before restore.
OriginalGriff 6-Jul-13 10:27am    
Make sure that every time you create an SqlConnection, it is Closed and Disposed when you have finished with it - it is the recommended practice anyway with any object that implements IDisposable, as SqlConnection, SqlCommand, and so forth all do.
I'm betting that at some point you create a connection, and then just let it go out of scope - which doesn't close it until the Garbage Collector gets in, or your program exits.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900