怎样在VB中实现附加分离数据库
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
'创建连接
Dim strcon As String = "integrated security=sspi;server=(local);database=master" Dim mycon As New SqlConnection(strcon) 1附加数据库 Dim mycmd2 As New SqlCommand mycmd2.Connection = mycon mycmd2.CommandType = CommandType.Text mycmd2.CommandText = "select * from sysdatabases" '打开连接 mycon.Open() '执行数据命令并绑定控件 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 '创建数据命令 Dim mycmd As New SqlCommand mycmd.Connection = mycon mycmd.CommandType = CommandType.StoredProcedure mycmd.CommandText = "sp_attach_db" '加入参数 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) '设置参数属性 name.Direction = ParameterDirection.Input '给参数赋值 name.Value = Trim(Me.TextBox1.Text) filename1.Value = Me.OpenFileDialog1.FileName.Replace(".mdf", ".ldf") filename2.Value = Me.OpenFileDialog1.FileName '打开连接 mycon.Open() '执行数据命令 Try mycmd.ExecuteNonQuery() MsgBox("数据库附加成功!") 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/分离数据库 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("很抱歉,你无权分离系统数据库!") Exit Sub End If '创建数据命令 Dim mycmd As New SqlCommand mycmd.Connection = mycon mycmd.CommandType = CommandType.StoredProcedure mycmd.CommandText = "sp_detach_db" '加入参数 Dim name As SqlParameter = mycmd.Parameters.Add("@dbname", SqlDbType.NVarChar) '设置参数属性 name.Direction = ParameterDirection.Input '给参数赋值 name.Value = Trim(Me.ComboBox1.SelectedItem) '打开连接 mycon.Open() '执行数据命令 Try mycmd.ExecuteNonQuery() MsgBox("数据库分离成功!") 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() '执行数据命令并绑定控件 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 编辑过 |
关键字查询
相关文章
正在查询... |