How To Create a SQL Server Database Programmatically by Using ADO.NET and Visual C++ .NET (307402)
The information in this article applies to:
- Microsoft ADO.NET (included with the .NET Framework 1.1)
- Microsoft ADO.NET (included with the .NET Framework) 1.0
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
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 TASKSUMMARY 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 <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;
- 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;
}
- 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 "C" and then press ENTER to create the database.
- Use the Server Explorer to verify that the database was
created.
back to the top
Additional Notes
back to the top
REFERENCES For additional information on the CREATE DATABASE
Transact-SQL command, see the SQL Server Books Online or MSDN Online Library: For more information on ADO.NET objects and syntax, see the
Microsoft .NET Framework SDK documentation or MSDN Online:
back to the top
Modification Type: | Minor | Last Reviewed: | 6/29/2004 |
---|
Keywords: | kbHOWTOmaster KB307402 kbAudDeveloper |
---|
|