Saturday, February 25, 2012

Creating New DB using SMO

Hi,
I am trying to create a new database using SMO, I am getting Set_Parent
Failure Error when try to create a new database.
This is the code I am using to create the database.

Database database= new Database(DbServer, "MyNewDB");
database.Create();
I know that if you just use the first line it will try to attach the database, and u will get set_parent failure error, but I am using .Create() also.... still I am getting the error.... can anyone help me in this ?
Thanks
Rujith

If above is the code you are using, I would think you would get a compile error. The Database Constructor overload that takes 2 parameters is the following:

Database(Server server, string name);

where you must give it a server object where the new database will reside.

Here is some sample code that works:

Server server = new Server(".");
Database db = new Database(server,"MyDatabaseName");
db.Create();

I have a few SMO tutorials that I have been slowly writing that may be of help:

SQL Server Management Objects (SMO) Tutorials

Regards,

Dave

|||

Dave,

Sorry for the wrong code, I removed the first line(creating server), while putting in the forum. Thats the reason for the syntax error. In my actual code, I am creating server first, then calling the database constructor.

Did you ever get the Set_parent failure error while working with SMO?

-Rujith

|||

You can create that error a couple of ways:

Database db = new Database(null,"MyDatabaseName");
db.Create();

or

Database db = new Database();
db.Parent = null;

Either way, if you pass in null for the server (parent) you will get the error:

SetParent failed for Database 'MyDatabaseName'.

Change your code to see if server is null before passing it into the constructor.

Regards,

Dave

No comments:

Post a Comment