This interface is akin to the SQL Server New Database menu.
In this presentation, I would like to show you how to create an SQL Server database using C#. Actually I had to deal with the problem when programming for our own specific DBMS.
First of all, you have to create a connection to the master database of your SQL Server to manipulate the new database (master is the database where you can get details about the whole DBMS).
Using the code
The code for creating the database is very simple, the main function can be listed as follows:
private void CreateDatabase(DatabaseParam DBParam)
tmpConn = new SqlConnection();
tmpConn.ConnectionString = "SERVER = " + DBParam.ServerName +
"; DATABASE = master; User ID = sa; Pwd = sa";
sqlCreateDBQuery = " CREATE DATABASE "
+ " ON PRIMARY "
+ " (NAME = " + DBParam.DataFileName +", "
+ " FILENAME = '" + DBParam.DataPathName +"', "
+ " SIZE = 2MB,"
+ " FILEGROWTH =" + DBParam.DataFileGrowth +") "
+ " LOG ON (NAME =" + DBParam.LogFileName +", "
+ " FILENAME = '" + DBParam.LogPathName + "', "
+ " SIZE = 1MB, "
+ " FILEGROWTH =" + DBParam.LogFileGrowth +") ";
SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn);
MessageBox.Show("Database has been created successfully!",
"Create Database", MessageBoxButtons.OK,
catch (System.Exception ex)
MessageBox.Show(ex.ToString(), "Create Database",
You have to change the
DBParam.ServerName to your appropriate SQL Server name.
Checking your results
To see the results, click on Enterprise Manager of SQL Server, click on the plus (+) next to your Server, click on (+) next to Database tab, and you can see the test DB.
Any comments or questions can be sent to: email@example.com.
I am Vietnamese. I am a researcher in Computer Science and a freelance translator in German and Vietnamese.
Willing to share experience!