Microsoft Knowledge Base Email Alertz

(305079) - Programmers often need to create databases programmatically. This article describes how to use ADO.NET and Visual Basic .NET to programmatically create a Microsoft SQL Server database. Steps to Create the Sample Create a new Visual Basic .NET Windows...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 305079 - Last Review: May 13, 2007 - Revision: 4.4

How to create a SQL Server database programmatically by using ADO.NET and Visual Basic .NET

This article was previously published under Q305079

On This Page

SUMMARY

Programmers often need to create databases programmatically. This article describes how to use ADO.NET and Visual Basic .NET to programmatically create a Microsoft 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

For additional information on the CREATE DATABASE Transact-SQL command, see the SQL Server Books Online or MSDN Online Library:
http://msdn.microsoft.com/en-us/library/aa258257.aspx (http://msdn.microsoft.com/en-us/library/aa258257.aspx)
For more information on ADO.NET objects and syntax, see the Microsoft .NET Framework SDK Documentation or MSDN Online:
Accessing Data with ADO.NET
http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx)
For more general information about ADO.NET or Visual Basic .NET, refer to the following MSDN newsgroups:
microsoft.public.dotnet.framework.adonet (http://msdn.microsoft.com/newsgroups/default.aspx?query=microsoft.public.dotnet.framework.adonet&dg=&cat=en-us-msdn&lang=en&cr=US&pt=&catlist=774F24A2-F71F-425F-AC2B-DC48AB0DA5C9&dglist=&ptlist=&exp=&sloc=en-us)

microsoft.public.dotnet.languages.vb (http://msdn.microsoft.com/newsgroups/default.aspx?query=microsoft.public.dotnet.languages.vb&dg=&cat=en-us-msdn&lang=en&cr=US&pt=&catlist=774F24A2-F71F-425F-AC2B-DC48AB0DA5C9&dglist=&ptlist=&exp=&sloc=en-us)

APPLIES TO
  • Microsoft ADO.NET 1.1
  • Microsoft ADO.NET 1.0
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbhowtomaster kbsqlclient kbsystemdata KB305079
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate