I want my VB .Net app to restore a SQL database

srimal

New member
Joined
Apr 28, 2006
Messages
4
Programming Experience
Beginner
can some help me to find a code to backup and restore a sql server 2000 database
 
i'm astounded that sqls2k doesnt come with some backup utilities you can invoke from the command line..
 
Backup and Restore MS SQL Server 2000 using VB.NET

VB.NET:
Dim cn as new sqlconnection
Dim cmd as new sqlcommand
 
Try
cn.connectionString = "Data Source=localhost;Initial Catalog=master;Trusted_Connection=True;Integrated Security=SSPI;"
cn.open()
 
cmd.connection = cn
cmd.commandText = "BACKUP DATABASE TestDB TO DISK='C:\yourbackupfilename.bak'"
cmd.ExecuteNonQuery()
 
Catch ex as Exception
Msgbox(ex.Message)
Finally
cmd.Dispose
cn.Dispose
cn = Nothing
cmd = Nothing
End Try
----------------------------------------------------------------
The above code assuming SQL Server located at localhost and the database name is TestDB. As for restoring, change the above code for cmd.CommandText to the following code.

VB.NET:
cmd.CommandText = "RESTORE DATABASE TestDB FROM DISK='C:\yourbackupfilename.bak'"

-----------------------------------------------------------------

Hope this will help. :)
 
Last edited by a moderator:
Hi, should this code work for a standalone .mdf file being used in a VB.NET 2005 application?

I get an error stating no entry in sysdatabse for my database name?
 
Oh, but it does.... osql and isql do a fine job of it.... but they can be unweildly to invoke from VB....

pachjo - ahh.... it maynot.... unless it's attached to a specific instance of a SQL Server....

-tg
 
Back
Top