How to create a dynamic database in asp.net in C#? Please help me. BTW I used 4-5 methods but didn't work for me.
One method is this:
String str;
SqlConnection myConn = new SqlConnection("Data Source=(LocalDB)\v11.0;Integrated security=True;"); //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%)";
SqlCommand myCommand = new SqlCommand(str, myConn);
try
{
myConn.Open();
myCommand.ExecuteNonQuery();
Label1.Text = "Database is created successfully";
}
catch (System.Exception ex)
{
Label1.Text = ex.Message;
}
finally
{
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
}
}