怎樣在VB中實現(xiàn)附加分離數(shù)據(jù)庫
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
'創(chuàng)建連接
Dim strcon As String = "integrated security=sspi;server=(local);database=master" Dim mycon As New SqlConnection(strcon) 1附加數(shù)據(jù)庫 Dim mycmd2 As New SqlCommand mycmd2.Connection = mycon mycmd2.CommandType = CommandType.Text mycmd2.CommandText = "select * from sysdatabases" '打開連接 mycon.Open() '執(zhí)行數(shù)據(jù)命令并綁定控件 Dim myreader As SqlDataReader myreader = mycmd2.ExecuteReader If myreader.HasRows Then While myreader.Read() If Me.TextBox1.Text = myreader(0) Then MessageBox.Show("很抱歉!", MessageBoxButtons.OK, MessageBoxIcon.Stop) Exit Sub End If End While End If mycon.Close() Dim filepath1 As String = "" OpenFileDialog1.Filter = "mdf文件|*.mdf" If OpenFileDialog1.ShowDialog = DialogResult.OK Then filepath1 = OpenFileDialog1.FileName End If '創(chuàng)建數(shù)據(jù)命令 Dim mycmd As New SqlCommand mycmd.Connection = mycon mycmd.CommandType = CommandType.StoredProcedure mycmd.CommandText = "sp_attach_db" '加入?yún)?shù) Dim name As SqlParameter = mycmd.Parameters.Add("@dbname", SqlDbType.NVarChar) Dim filename1 As SqlParameter = mycmd.Parameters.Add("@filename1", SqlDbType.NVarChar) Dim filename2 As SqlParameter = mycmd.Parameters.Add("@filename2", SqlDbType.NVarChar) '設置參數(shù)屬性 name.Direction = ParameterDirection.Input '給參數(shù)賦值 name.Value = Trim(Me.TextBox1.Text) filename1.Value = Me.OpenFileDialog1.FileName.Replace(".mdf", ".ldf") filename2.Value = Me.OpenFileDialog1.FileName '打開連接 mycon.Open() '執(zhí)行數(shù)據(jù)命令 Try mycmd.ExecuteNonQuery() MsgBox("數(shù)據(jù)庫附加成功!") Me.ComboBox1.Items.Add(Trim(Me.TextBox1.Text)) Me.TextBox1.Text = "" Catch ex As Exception MessageBox.Show(ex.Message, "請注意!", MessageBoxButtons.OK, MessageBoxIcon.Stop) Finally '關閉連接 mycon.Close() End Try 2/分離數(shù)據(jù)庫 If Me.ComboBox1.SelectedItem = "master" Or Me.ComboBox1.SelectedItem = "model" Or Me.ComboBox1.SelectedItem = "msdb" Or Me.ComboBox1.SelectedItem = "Northwind" Or Me.ComboBox1.SelectedItem = "pubs" Or Me.ComboBox1.SelectedItem = "tempdb" Then MessageBox.Show("很抱歉,你無權分離系統(tǒng)數(shù)據(jù)庫!") Exit Sub End If '創(chuàng)建數(shù)據(jù)命令 Dim mycmd As New SqlCommand mycmd.Connection = mycon mycmd.CommandType = CommandType.StoredProcedure mycmd.CommandText = "sp_detach_db" '加入?yún)?shù) Dim name As SqlParameter = mycmd.Parameters.Add("@dbname", SqlDbType.NVarChar) '設置參數(shù)屬性 name.Direction = ParameterDirection.Input '給參數(shù)賦值 name.Value = Trim(Me.ComboBox1.SelectedItem) '打開連接 mycon.Open() '執(zhí)行數(shù)據(jù)命令 Try mycmd.ExecuteNonQuery() MsgBox("數(shù)據(jù)庫分離成功!") Me.ComboBox1.Items.Remove(Trim(Me.ComboBox1.SelectedItem)) Catch ex As Exception MessageBox.Show(ex.Message, "請注意!", MessageBoxButtons.OK, MessageBoxIcon.Stop) Finally '關閉連接 mycon.Close() End Try End Sub Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.ComboBox1.Text = "" Dim mycmd1 As New SqlCommand mycmd1.Connection = mycon mycmd1.CommandType = CommandType.Text mycmd1.CommandText = "select * from sysdatabases" '打開連接 mycon.Open() '執(zhí)行數(shù)據(jù)命令并綁定控件 Dim myreader As SqlDataReader myreader = mycmd1.ExecuteReader If myreader.HasRows Then While myreader.Read() Me.ComboBox1.Items.Add(myreader(0)) End While End If mycon.Close() End Sub 該文章在 2014/3/25 1:14:35 編輯過 |
關鍵字查詢
相關文章
正在查詢... |