如何在vb.net中的指定文件夹中创建数据库 [英] How to create a database in specified folder in vb.net

查看:83
本文介绍了如何在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

推荐答案

Dim str As String



Dim myConn As SqlConnection = New SqlConnection(Server =(local)\\\
etsdk;&_

uid = sa; pwd =; database = master)



str =在初级创建数据库MyDatabase&_

(NAME = MyDatabase_Data,&_

FILENAME ='D:\ MyFolder \ MyDatabaseData.mdf',&_

SIZE = 2MB,&_

MAXSIZE = 10MB,&_

FILEGROWTH = 10%&_

登录&_

(NAME = MyDatabase_Log,&_

FILENAME ='D:\ MyFolder \ MyDatabaseLog.ldf',&_

SIZE = 1MB,&_

MAXSIZE = 5MB,&_

FILEGROWTH = 10%)



Dim myCommand As SqlCommand =新的SqlCommand(str,myConn)



尝试

myConn.Open()

myCommand.ExecuteNonQuery ()

MessageBox.Show(数据库创建成功,_

MyProgram,MessageBoxButtons.OK,_

MessageBoxIcon.Information )

Catch ex As Exception

MessageBox.Show(ex.ToString())

最后

如果( myConn.State = ConnectionState.Open)然后

myConn.Close()

结束如果

结束尝试
Dim str As String

Dim myConn As SqlConnection = New SqlConnection("Server=(local)\netsdk;" & _
"uid=sa;pwd=;database=master")

str = "CREATE DATABASE MyDatabase ON PRIMARY " & _
"(NAME = MyDatabase_Data, " & _
" FILENAME = 'D:\MyFolder\MyDatabaseData.mdf', " & _
" SIZE = 2MB, " & _
" MAXSIZE = 10MB, " & _
" FILEGROWTH = 10%) " & _
" LOG ON " & _
"(NAME = MyDatabase_Log, " & _
" FILENAME = 'D:\MyFolder\MyDatabaseLog.ldf', " & _
" SIZE = 1MB, " & _
" MAXSIZE = 5MB, " & _
" FILEGROWTH = 10%) "

Dim myCommand As SqlCommand = New SqlCommand(str, myConn)

Try
myConn.Open()
myCommand.ExecuteNonQuery()
MessageBox.Show("Database is created successfully", _
"MyProgram", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
If (myConn.State = ConnectionState.Open) Then
myConn.Close()
End If
End Try


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆