本文介绍了如何在vb.net中的指定文件夹中创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个数据库。数据库名称将是savefiledialog文件名。它将在savefiledialog的选定目录上创建。 (我在表单上添加了savefiledialog)。下面的代码在MSSQL默认文件夹中创建数据库。帮助PLZ



I want to create a database. the database name will be savefiledialog file name. And it will create on selected directory of savefiledialog. (i have added a savefiledialog on my form). Following code creating database in MSSQL default folder. Help plz

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click

        Dim filename, dbPath As String

        SaveFileDialog1.Filter = "MS SQL Database File (*.mdf*)|*.mdf"
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            dbPath = SaveFileDialog1.FileName
            filename = System.IO.Path.GetFileNameWithoutExtension(dbPath)
            Dim str As String
            Dim myConn = New SqlConnection("Data Source=.\SQLEXPRESS;Integrated security=true;database=")

            str = "CREATE DATABASE " + filename + ";"
            Dim myCommand = New SqlCommand(str, myConn)
            Try
                myConn.Open()
                myCommand.ExecuteNonQuery()
                MessageBox.Show("DataBase is Created Successfully", "New Database", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                MessageBox.Show(ex.ToString(), "New Database", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Finally
                If (myConn.State = ConnectionState.Open) Then
                    myConn.Close()
                End If
            End Try
        End If

    End Sub

推荐答案


这篇关于如何在vb.net中的指定文件夹中创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 14:57