Page 1 of 1

Create a database when it doesn't exists yet

Posted: Thu 03 May 2012 21:37
by ashlar64
Hello,

Is there a way to create a new database?
I have this code:

string connectionString = "Data Source=(local);Database=mTest;Integrated Security=True;Persist Security Info=True";

MContext m = new MContext(connectionString);
m.Connection.Open();
m.CreateDatabase(true, true);

I cannot open it because the mTest database doesn't exist yet.

Re: Create a database when it doesn't exists yet

Posted: Mon 07 May 2012 11:17
by MariiaI
The problem is that the connection cannot be opened because the database 'mTest' does not exist.
So, you can try the following way: do not specify the database in the connection string and then perform CreateDatabase method.

For example:

Code: Select all

string  connectionString = "Data Source=(local);Integrated Security=True;Persist Security Info=True";
MContext m = new MContext(connectionString);
m.Connection.Open();
m.CreateDatabase(true, true);


For more information about database creation see:
http://www.devart.com/linqconnect/docs/DDL.html