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

Create a SQL Server Database programmatically

· 52CE 我爱测

This article describes how to create a SQL Server Database programmatically by using ADO.NET and Visual C# .NET.

This article describes how to create a SQL Server Database programmatically by using ADO.NET and Visual C# .NET.

Original product version:   Visual C#
Original KB number:   307283

Summary

This step-by-step article shows you how to create a Microsoft SQL Server database by using ADO.NET and Visual C# .NET because programmers often need to create Databases programmatically.

This article refers to the following Microsoft .NET Framework Class Library namespace: System.Data.SqlClient.

Steps to create a SQL Server Database

To create the database, follow these steps:

  1. Create a new Visual C# .NET Windows application.

  2. Place a button on Form1. Change the button's Name property to btnCreateDatabase, and then change the Text property to Create Database.

  3. Use the using statement on the System and System.Data namespaces so that you do not have to qualify declarations in those namespaces later in your code. Add the following code to the General Declarations section of Form1:

    using System;
    using System.Data.SqlClient;
    
  4. Switch to Form view, and then double-click Create Database to add the click event handler. Add the following sample code to the handler:

     String str;
     SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");
    
    str = "CREATE DATABASE MyDatabase ON PRIMARY " +
     "(NAME = MyDatabase_Data, " +
     "FILENAME = 'C:\\MyDatabaseData.mdf', " +
     "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%)" +
     "LOG ON (NAME = MyDatabase_Log, " +
     "FILENAME = 'C:\\MyDatabaseLog.ldf', " +
     "SIZE = 1MB, " +
     "MAXSIZE = 5MB, " +
     "FILEGROWTH = 10%)";
    
    SqlCommand myCommand = new SqlCommand(str, myConn);
    try
    {
        myConn.Open();
        myCommand.ExecuteNonQuery();
        MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    finally
    {
        if (myConn.State == ConnectionState.Open)
        {
            myConn.Close();
        }
    }
    
  5. Change the connection string to point to your computer running SQL Server, and then verify that the Database argument is set to Master or is blank.

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

  7. Use the Server Explorer to verify that the database is created.

Note:

  • This code creates a custom database with specific properties.
  • The folder that is going to 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 the SQL Server Model database, and you want the database in the default location, then change the str variable in the code, as in the following sample 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 10/26/2020
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

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

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