Create a database when it doesn't exists yet

Create a database when it doesn't exists yet

Postby ashlar64 » Thu 03 May 2012 21:37

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.
ashlar64
 
Posts: 75
Joined: Thu 04 May 2006 18:56

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

Postby MariiaI » Mon 07 May 2012 11:17

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
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17


Return to LinqConnect (LINQ to SQL support)