vb.net 在运行时以编程方式创建 sql server 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25011969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a sql server database programmatically at run time
提问by Coder92
I am creating a vb .net winform project that uses a sql server 2008 database. I have the project more or less finished but I want to tweak it a bit. One of the tweaks is as follows. I have created the database within sql server 2008 but I am wondering how do I go about programmatically creating the database within the vb .net project. I have searched the internet on this matter but nothing is clear enough for me. Do I create the database within the opening form or do I create it in a separate class and call it in the other forms I am using the database in? Any help with this matter would be greatly appreciated.
我正在创建一个使用 sql server 2008 数据库的 vb .net winform 项目。我的项目或多或少已经完成,但我想稍微调整一下。其中一项调整如下。我已经在 sql server 2008 中创建了数据库,但我想知道如何在 vb .net 项目中以编程方式创建数据库。我已经在互联网上搜索过这个问题,但对我来说没有什么足够清楚。我是在打开的表单中创建数据库还是在单独的类中创建它并在我使用数据库的其他表单中调用它?对此事的任何帮助将不胜感激。
回答by Suji
The following code will help you to create a database named my_db and a table within the database named as customer.
以下代码将帮助您创建一个名为 my_db 的数据库和一个名为 customer 的数据库中的表。
Note:necessary explanations are given in comments please read it for clarification
注意:评论中给出了必要的解释,请阅读以澄清
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
//creating and initializing the connection string
Dim myConnectionString As SqlConnection = New SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;Pooling=False")
//since we need to create a new database set the Initial Catalog as Master
//Which means we are creating database under master DB
Dim myCommand As String //to store the sql command to be executed
myCommand = "CREATE database my_db" //the command that creates new database
Dim cmd As SqlCommand = New SqlCommand(myCommand, myConnectionString) // creating command for execution
Try
cmd.Connection.Open() //open a connection with cmd
cmd.ExecuteNonQuery() //Execute the query
cmd.Connection.Close() //Close the connection
Catch
MsgBox(" Already installed database", MsgBoxStyle.Critical, " MaS InfoTech- Warning")
End Try
//Creating table to the dynamicaly created database
Try
Dim cn As SqlConnection = New SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=my_db;Integrated Security=True;Pooling=False")
//here the connection string is initialized with Initial Catalog as my_db
Dim sql As String //sql query string
sql = "CREATE TABLE customer(cus_name varchar(50) NULL,address varchar(50) NULL,mobno numeric(18, 0) NULL,tin varchar(50) NULL,kg varchar(50) NULL)"
cmd = New SqlCommand(sql, cn) // create command with connection and query string
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
Catch
MsgBox(" Already installed database", MsgBoxStyle.Critical, " MaS InfoTech- Warning")
End Try
End Sub

