0 Replies - 5527 Views - Last Post: 14 January 2013 - 04:47 AM Rate Topic: -----

#1 bibang  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 13-November 12

restore and back up database using VB .net (sql 2008 & vb 2010)

Posted 14 January 2013 - 04:47 AM

im just a rookie on VB .net and sql server and i found this code for restore and backup database using VB .net, but i have questions regarding on some parts of the code where im not so familiar, :

my questions are just on the bold part of the code:

1st: the server(".") & server(".\sqlexpress") , are these servers default or i have to chagne this? all i know is when i connect to ms sql 2008, i use server .\JJA

2nd: cmd = New SqlCommand("select * from sysservers where srvproduct='SQL Server'", con) , im not so familiar, what should i put next to "from"(sysservers) and "where"(srvproduct='SQL Server')?

last: New SqlCommand("select * from sysdatabases", con), also also next to "from"(sysdatabases)?

thank you for your help, thanks a lot

here is the code:

Imports System.Data.SqlClient
 
Public Class Form1
    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim dread As SqlDataReader
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        [b]server(".")
        server(".\sqlexpress")[/b]
    End Sub
   
     Sub server(ByVal str As String)
        [b]con = New SqlConnection("Data Source=" & str & ";Database=Master;integrated security=SSPI;")[/b]
        con.Open()
        [b]cmd = New SqlCommand("select *  from sysservers  where srvproduct='SQL Server'", con)[/b]
        dread = cmd.ExecuteReader
        While dread.Read
            cmbserver.Items.Add(dread(2))
        End While
        dread.Close()
    End Sub
    
    Sub connection()
        [b]con = New SqlConnection("Data Source=" & Trim(cmbserver.Text) & ";Database=Master;integrated security=SSPI;")[/b]
        con.Open()
        cmbdatabase.Items.Clear()
        [b]cmd = New SqlCommand("select * from sysdatabases", con)[/b]
        dread = cmd.ExecuteReader
        While dread.Read
            cmbdatabase.Items.Add(dread(0))
        End While
        dread.Close()
    End Sub
    
    Private Sub cmbserver_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbserver.SelectedIndexChanged
        connection()
    End Sub
   
    Sub query(ByVal que As String)
        On Error Resume Next
        cmd = New SqlCommand(que, con)
        cmd.ExecuteNonQuery()
    End Sub
   
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If ProgressBar1.Value = 100 Then
            Timer1.Enabled = False
            ProgressBar1.Visible = False
            MsgBox("Successfully Done")
        Else
            ProgressBar1.Value = ProgressBar1.Value + 5
        End If
    End Sub
    
    Sub blank(ByVal str As String)
        If cmbserver.Text = "" Or cmbdatabase.Text = "" Then
            MsgBox("Server Name & Database Blank Field")
            Exit Sub
        Else
            If str = "backup" Then
                SaveFileDialog1.FileName = cmbdatabase.Text
                SaveFileDialog1.ShowDialog()
                Timer1.Enabled = True
                ProgressBar1.Visible = True
                Dim s As String
                s = SaveFileDialog1.FileName
                query("backup database " & cmbdatabase.Text & " to disk='" & s & "'")
            ElseIf str = "restore" Then
                OpenFileDialog1.ShowDialog()
                Timer1.Enabled = True
                ProgressBar1.Visible = True
                query("RESTORE DATABASE " & cmbdatabase.Text & " FROM disk='" & OpenFileDialog1.FileName & "'")
            End If
        End If
    End Sub
    
    Private Sub cmbbackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbbackup.Click
        blank("backup")
    End Sub
    
    Private Sub cmdrestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdrestore.Click
        blank("restore")
    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Page 1 of 1