Microsoft Knowledge Base Email Alertz

KBAlertz.com: (307402) - Programmers often need to create databases programmatically. This article describes how to use ADO.NET and Visual C++ .NET to programmatically create a Microsoft SQL Server database. Steps to Create the Sample Start Microsoft Visual Studio .NET, and...

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]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

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




Article ID: 307402 - Last Review: May 13, 2007 - Revision: 3.3

How To Create a SQL Server Database Programmatically by Using ADO.NET and Visual C++ .NET

This article was previously published under Q307402

On This Page

SUMMARY

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

Steps to Create the Sample

  1. Start Microsoft Visual Studio .NET, and create a new Managed C++ Application project. Form1 is added to the project by default.
  2. Add the following code before your Main function definition:
    #using <mscorlib.dll>
    using namespace System;
    
    #using <system.dll>
    using namespace System;
    
    #using <System.data.dll>
    using namespace System::Data;
    using namespace System::Data::SqlClient;
    
    #using <system.windows.forms.dll>
    using namespace System::Windows::Forms;
    					
  3. Add the following code in your Main function:
    int main(void)
    {
       Console::WriteLine(S"Press 'C' and then ENTER to create a new database");
       Console::WriteLine(S"Press any other key and then ENTER to quit");
       char c = Console::Read();
       if (c == 'C' || c == 'c')
       {
           Console::WriteLine(S"Creating the database...");
           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%)";
    
           try
           {
               SqlCommand* myCommand = new SqlCommand(str, myConn);
               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);
           }
    
           if (myConn->State == ConnectionState::Open)
           {
               myConn->Close();
           }
        }
    
         return 0;
    }
    					
  4. Change the connection string to point to your SQL Server, and make sure that the Database argument is set to Master or blank.
  5. Press the F5 key or the CTRL+F5 key combination to run the project. Press "C" and then press ENTER to create the database.
  6. Use the Server Explorer to verify that the database was created.

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://msdn2.microsoft.com/en-us/library/aa258257(SQL.80).aspx (http://msdn2.microsoft.com/en-us/library/aa258257(SQL.80).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)

APPLIES TO
  • Microsoft ADO.NET 1.1
  • Microsoft ADO.NET 1.0
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
Keywords: 
kbhowtomaster KB307402
       

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

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please