Showing posts with label smo. Show all posts
Showing posts with label smo. Show all posts

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

Sunday, February 19, 2012

Creating foreign key if exists.

I have the following SMO code:

// Create options

ScriptingOptions so = new ScriptingOptions();

so.IncludeIfNotExists = true;

so.DriAll = true;

// For scripting check constraints

so.DriChecks = true;

// For scripting extended properties

so.ExtendedProperties = true;

// Don't allow system objects

so.AllowSystemObjects = false;

StringCollection individualForeignKeyCreation = foreignKey.Script(so);

Notice that the scripting option is set to include if the table exists. But the script that is generated is like:

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ReasonCodeSubCategory_ReasonCodeCategory]') AND parent_object_id = OBJECT_ID(N'[dbo].[ReasonCodeSubCategory]'))
ALTER TABLE [dbo].[ReasonCodeSubCategory] WITH CHECK ADD CONSTRAINT [FK_ReasonCodeSubCategory_ReasonCodeCategory] FOREIGN KEY([ReasonCodeCategoryID])
REFERENCES [ReasonCodeCategory] ([ReasonCodeCategoryID])
ALTER TABLE [dbo].[ReasonCodeSubCategory] CHECK CONSTRAINT [FK_ReasonCodeSubCategory_ReasonCodeCategory]

If "if" condition takes care of the first line but it seems that there should be a BEGIN END so the "if" condition holds for both ALTER statements. The reason this came up is when I try to run this script I get an error. I could modify the string collection but even this has been made difficult because it seems the 'if" and the first ALTER statement are one string in the collection returned from Script.

Any suggestions?

Thank you.

Kevin

Kevin,

Thank you for reporting this issue. This is a defect in SMO. I opened a bug report and we will fix it in the next realease (the fix will likely appear in one of the upcoming CTP releases)