大网数据广告活动
Web服务器与PHP

Create SQL Server database programmatically

· 52CE 我爱测

This article describes how to use ADO.NET and Visual Basic .NET to programmatically create a Microsoft SQL Server database.

This article describes how to use ADO.NET and Visual Basic .NET to programmatically create a Microsoft SQL Server database.

Original product version:   ADO.NET, Visual Basic .NET
Original KB number:   305079

Summary

Programmers often need to create databases programmatically. This article describes how to use ADO.NET and Visual Basic .NET to programmatically create a SQL Server database.

Steps to create the sample

  1. Create a new Visual Basic .NET Windows Application project. Form1 is added to the project by default.

  2. Place a Command button on Form1, and change its Name property to btnCreateDatabase and its Text property to Create Database.

  3. Copy and paste the following line of code into Form1's "general declaration" section:

    Imports System.Data.SqlClient
    
  4. Copy and paste the following code after the region "Windows Form Designer generated code":

    Private Sub btnCreateDatabase_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnCreateDatabase.Click
    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
    
    End Sub
    
  5. Change the connection string to point to your SQL Server, and make sure that the Database argument is set to Master or blank.

  6. Press F5 or CTRL+F5 to run the project, and then click Create Database.

Additional notes

  • This code creates a custom database with specific properties.

  • The folder that will hold the created .mdf and .ldf files must already exist before you run the code or an exception will be generated.

  • If you want to create a database that is similar to SQL Server's Model database and in the default location, then change the str variable in the code:

    str = "CREATE DATABASE MyDatabase"
    

References

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 08/27/2024
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。