Sunday, February 19, 2012

Database.Create()

I'm looking at using SQL Server SMO to create databases dynamically from a code generator that I'm writing that will use UML class diagrams of business entities as the input metadata.

Is there a sample for using the Microsoft.SqlServer.Management.Smo.Database class, and specifically the Create() method.

When I try a simple call to Create() after merely assigning the database a name, I recieve an inner exception stating:

"You cannot perform operation Create on an object in state Pending."

Any help would be greatly appreciated.

- Doug

There are two ways doing this:


Server svr = new Server(m_yukon);

Database db1, db2;

if ((db1 = svr.Databases["db1"]) != null)
db1.Drop();
if ((db2 = svr.Databases["db2"]) != null)
db2.Drop();

db1 = new Database(svr, "db1");
db1.Create();

db2 = new Database();
db2.Name = "db2";
db2.Parent = svr;
db2.Create();