Microsoft KB Archive/307402

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

Article ID: 307402

Article Last Modified on 5/13/2007

-

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

-



This article was previously published under Q307402



For a Microsoft Visual Basic .NET version of this article, see 305079.

For a Microsoft Visual C# .NET version of this article, see 307283.

For a Microsoft Visual J# .NET version of this article, see 320628.

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

IN THIS TASK
SUMMARY
 * Steps to Create the Sample
 * Additional Notes

REFERENCES



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.

back to the top

Steps to Create the Sample
 Start Microsoft Visual Studio .NET, and create a new Managed C++ Application project. Form1 is added to the project by default.  Add the following code before your Main function definition: using namespace System;
 * 1) using 

using namespace System;
 * 1) using 

using namespace System::Data; using namespace System::Data::SqlClient;
 * 1) using 

using namespace System::Windows::Forms;   Add the following code in your Main function: int main(void) {  Console::WriteLine(S&quot;Press 'C' and then ENTER to create a new database&quot;); Console::WriteLine(S&quot;Press any other key and then ENTER to quit&quot;); char c = Console::Read; if (c == 'C' || c == 'c') {      Console::WriteLine(S&quot;Creating the database...&quot;); String* str; SqlConnection* myConn = new SqlConnection (&quot;Server=localhost;Integrated security=SSPI;database=master&quot;); str = &quot;CREATE DATABASE MyDatabase ON PRIMARY &quot; &quot;(NAME = MyDatabase_Data, &quot;           &quot;FILENAME = 'C:\\MyDatabaseData.mdf', &quot;            &quot;SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) &quot; &quot;LOG ON (NAME = MyDatabase_Log, &quot;           &quot;FILENAME = 'C:\\MyDatabaseLog.ldf', &quot;            &quot;SIZE = 1MB, &quot;            &quot;MAXSIZE = 5MB, &quot;            &quot;FILEGROWTH = 10%)&quot;;
 * 1) using 

try {          SqlCommand* myCommand = new SqlCommand(str, myConn); myConn->Open; myCommand->ExecuteNonQuery; MessageBox::Show(&quot;Database is created successfully&quot;,                            &quot;MyProgram&quot;, MessageBoxButtons::OK,                             MessageBoxIcon::Information); }      catch (System::Exception* ex) {          MessageBox::Show(ex->ToString, &quot;MyProgram&quot;,                                MessageBoxButtons::OK,                             MessageBoxIcon::Information); }

if (myConn->State == ConnectionState::Open) {          myConn->Close; }   }

return 0; }                    Change the connection string to point to your SQL Server, and make sure that the Database argument is set to Master or blank. Press the F5 key or the CTRL+F5 key combination to run the project. Press &quot;C&quot; and then press ENTER to create the database.</li> Use the Server Explorer to verify that the database was created.</li></ol>

back to the top

Additional Notes
<ul> This code creates a custom database with specific properties.</li> 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.</li>  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 = &quot;CREATE DATABASE MyDatabase&quot; </li></ul>

back to the top

<div class="references_section">