create a sql server database programmatically

Solutions on MaxInterview for create a sql server database programmatically by the best coders in the world

showing results for - "create a sql server database programmatically"
Domenico
30 May 2020
1 String str;
2 SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");
3
4str = "CREATE DATABASE MyDatabase ON PRIMARY " +
5 "(NAME = MyDatabase_Data, " +
6 "FILENAME = 'C:\\MyDatabaseData.mdf', " +
7 "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%)" +
8 "LOG ON (NAME = MyDatabase_Log, " +
9 "FILENAME = 'C:\\MyDatabaseLog.ldf', " +
10 "SIZE = 1MB, " +
11 "MAXSIZE = 5MB, " +
12 "FILEGROWTH = 10%)";
13
14SqlCommand myCommand = new SqlCommand(str, myConn);
15try
16{
17    myConn.Open();
18    myCommand.ExecuteNonQuery();
19    MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
20}
21catch (System.Exception ex)
22{
23    MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
24}
25finally
26{
27    if (myConn.State == ConnectionState.Open)
28    {
29        myConn.Close();
30    }
31}
32
Theo
19 Sep 2019
1String str;
2 SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");
3
4str = "CREATE DATABASE MyDatabase ON PRIMARY " +
5 "(NAME = MyDatabase_Data, " +
6 "FILENAME = 'C:\\MyDatabaseData.mdf', " +
7 "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%)" +
8 "LOG ON (NAME = MyDatabase_Log, " +
9 "FILENAME = 'C:\\MyDatabaseLog.ldf', " +
10 "SIZE = 1MB, " +
11 "MAXSIZE = 5MB, " +
12 "FILEGROWTH = 10%)";
13
14SqlCommand myCommand = new SqlCommand(str, myConn);
15try
16{