LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

怎样在VB中实现附加分离数据库

admin
2014年3月25日 1:14 本文热度 5552
'创建连接
    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 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved