Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Taking backup of SQL Server 2000 database in VB.Net 2005?
 

Taking backup of SQL Server 2000 database in VB.Net 2005?

I am developing a windows application in VB.Net and database is SQL server 2000.
In this I want to give facility to take back of database.
How to do this?

I want to do through coding in VB.Net.

Harsh1  Saturday, May 19, 2007 5:17 AM

Hi Harsh,

To backup the database using VB.NET

1) Connect to the database thatyou use in the application, or connect to themaster database. The master database is recommended.

2)Backup thedatabase using the SQL statement.

below is a sample code for your information.

Code Snippet
Sub backupDb()

Dim mysqlcomm As SqlClient.SqlCommand

Dim strsql As String

Dim strbak As String

Try

If backupDir <> Nothing Then

If Not System.IO.Directory.Exists(backupDir) Then

System.IO.Directory.CreateDirectory(backupDir)

End If

Else

If Not System.IO.Directory.Exists(Application.StartupPath + "\backup") Then

System.IO.Directory.CreateDirectory(Application.StartupPath + "\backup")

End If

backupDir = Application.StartupPath + "\backup"

End If

strbak = backupDir

strsql = "backup database myDataBase to disk='" + strbak + "\myDataBase.bak' with name='myDataBase backup all',description='Full Backup Of myDataBase'" 'SQL statement for backup

'strsql = "RESTORE DATABASE { database_name | @database_name_var } [ FROM < backup_device > [ ,...n ] ] " 'SQL statement for restoring

mysqlconn.Open()

mysqlcomm = New SqlClient.SqlCommand(strsql, mysqlconn)

mysqlcomm.ExecuteNonQuery()

mysqlconn.Close()

Catch ex As Exception

If Not mysqlconn.State.Closed = ConnectionState.Closed Then

mysqlconn.Close()

End If

Throw ex

End Try

End Sub

Sub open_Masterconn()

Dim sqlconnStr As String

Try

sqlconnStr = "data source = " + DbHost + " ; initial catalog = master; user id = " + connUser + "; password = " + connpwd

mysqlconn = New SqlClient.SqlConnection(sqlconnStr)

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

Hope this helps.

Regards

Rong-Chun Zhang  Tuesday, May 22, 2007 1:29 PM
Please give me the code to take backup of sql server database in Vb.Net 2005.
Harsh1  Tuesday, May 22, 2007 5:47 AM

Hi Harsh,

To backup the database using VB.NET

1) Connect to the database thatyou use in the application, or connect to themaster database. The master database is recommended.

2)Backup thedatabase using the SQL statement.

below is a sample code for your information.

Code Snippet
Sub backupDb()

Dim mysqlcomm As SqlClient.SqlCommand

Dim strsql As String

Dim strbak As String

Try

If backupDir <> Nothing Then

If Not System.IO.Directory.Exists(backupDir) Then

System.IO.Directory.CreateDirectory(backupDir)

End If

Else

If Not System.IO.Directory.Exists(Application.StartupPath + "\backup") Then

System.IO.Directory.CreateDirectory(Application.StartupPath + "\backup")

End If

backupDir = Application.StartupPath + "\backup"

End If

strbak = backupDir

strsql = "backup database myDataBase to disk='" + strbak + "\myDataBase.bak' with name='myDataBase backup all',description='Full Backup Of myDataBase'" 'SQL statement for backup

'strsql = "RESTORE DATABASE { database_name | @database_name_var } [ FROM < backup_device > [ ,...n ] ] " 'SQL statement for restoring

mysqlconn.Open()

mysqlcomm = New SqlClient.SqlCommand(strsql, mysqlconn)

mysqlcomm.ExecuteNonQuery()

mysqlconn.Close()

Catch ex As Exception

If Not mysqlconn.State.Closed = ConnectionState.Closed Then

mysqlconn.Close()

End If

Throw ex

End Try

End Sub

Sub open_Masterconn()

Dim sqlconnStr As String

Try

sqlconnStr = "data source = " + DbHost + " ; initial catalog = master; user id = " + connUser + "; password = " + connpwd

mysqlconn = New SqlClient.SqlConnection(sqlconnStr)

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

Hope this helps.

Regards

Rong-Chun Zhang  Tuesday, May 22, 2007 1:29 PM
Sir i have created a web application using .net2003 and sql2000 but after running the said application in .net2005 and sql2000 all the images related stuffs i.e. insertion and deletion stopped working please help me out of this
regards
zafarbdk  Thursday, August 06, 2009 2:28 PM

You can use google to search for other answers

Custom Search

More Threads

• How to resize ComboBox?
• Ensuring minimum size of UserControl
• How to customize the selection border of selected control in VS2005 (WinForm)
• How to make property associated with an UITYpeEditor Readonly?
• BindingNavigator disappears from Container UserControl at Design Time
• CodeDomSerializer and resx
• openFileDialog (help with Declaratoin) Thxs
• Tablelayout Panel inherit problem
• Working with TreeNodes...
• BaseControlArray in VB.Net, help please