2010-04-30 34 views

回答

1
Imports System.IO 
Imports System.Data.SqlClient 

Public Class Form2 
    Dim con As SqlConnection 
    Dim cmd As SqlCommand 
    Dim dread As SqlDataReader 
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     server(".") 
     server(".\sqlexpress") 
    End Sub 

    Sub server(ByVal str As String) 
     con = New SqlConnection("Data Source=" & str & ";Database=Master;integrated security=SSPI;") 
     con.Open() 
     cmd = New SqlCommand("select * from sysservers where srvproduct='SQL Server'", con) 
     dread = cmd.ExecuteReader 
     While dread.Read 
      ComboBox1.Items.Add(dread(2)) 
     End While 
     dread.Close() 
     con.Close() 
    End Sub 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
     If ComboBox1.Text = "" Then 
      MsgBox("select the server name") 
      ComboBox1.Focus() 
      Exit Sub 
     End If 
     Try 
      ComboBox2.Text = "" 
      con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;") 
      con.Open() 
      If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then 
       ComboBox2.Text = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName) 
       cmd = New SqlCommand("drop database " & ComboBox2.Text & " ", con) 
       cmd.ExecuteNonQuery() 
       cmd = New SqlCommand("RESTORE DATABASE " & ComboBox2.Text & " FROM disk='" & OpenFileDialog1.FileName & "'", con) 
       cmd.ExecuteNonQuery() 
       MsgBox("Restore Successfully Completed") 
       ComboBox1.Text = "" 
       ComboBox2.Text = "" 
      Else 
       MsgBox("Restore Cancelled") 
      End If 
      con.Close() 
     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 
    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     If ComboBox1.Text = "" Then 
      MsgBox("select the server name") 
      ComboBox1.Focus() 
      Exit Sub 
     ElseIf ComboBox2.Text = "" Then 
      MsgBox("select the database name") 
      ComboBox2.Focus() 
      Exit Sub 
     End If 
     Try 
      con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;") 
      con.Open() 
      SaveFileDialog1.FileName = ComboBox2.Text + ".bak" 
      If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then 
       Dim s As String 
       s = SaveFileDialog1.FileName 
       cmd = New SqlCommand("backup database " & ComboBox2.Text & " to disk='" & s & "'", con) 
       cmd.ExecuteNonQuery() 
       MsgBox("Back Up Successfully Created") 
       ComboBox1.Text = "" 
       ComboBox2.Text = "" 
       con.Close() 
      Else 
       MsgBox("Back Up Cancelled") 
      End If 
     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 
    End Sub 

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged 
     con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;") 
     con.Open() 
     ComboBox2.Items.Clear() 
     cmd = New SqlCommand("select * from sysdatabases", con) 
     dread = cmd.ExecuteReader 
     While dread.Read 
      ComboBox2.Items.Add(dread(0)) 
     End While 
     dread.Close() 
     con.Close() 
    End Sub 
End Class 
相關問題