SMO Changes in September CTP?
Does anyone know if the way SMO works change in the September CTP of SQL Server 2005? The reason I ask is that I’m running the code below and it is throwing an exception at the Drop() method call rather than dropping the database as also shown below.
Server server = new Server(cboServers.Text);
Database db = new Database(server, cboDatabases.Text);
db.Drop();
Causes this exception:
Microsoft.SqlServer.Management.Smo.FailedOperationException occurred
HelpLink="http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.1314.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Drop+Database&LinkId=20476"
Message="Drop failed for Database 'Carol'. "
Source="Microsoft.SqlServer.Smo"
Operation="Drop"
StackTrace:
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.DropImpl()
at Microsoft.SqlServer.Management.Smo.Database.Drop()
at UglySMO.UglySMO.cmdDeleteDatabase_Click(Object sender, EventArgs e) in E:\Documents\Development\Exploring SQL Server 2005\Code\SMO\UglySMO\UglySMO\UglySMO.cs:line 360
If I drill into the inner execption, it says "You cannot execute this operation since the object has not been created." Yes, it has! Management Studio shows the Carol database, and I can delete it from there with no problem.
As near as I can tell the problem is that the Database object, db, doesn’t have a connection to the server. Maybe because it isn’t fully instantiated? This is different behavior than when I wrote the code, for the August CTP I think. And BOL is behind on the documentation of SMO.
Any ideas what might be wrong?
Thanks!
Update: Thanks to darshan at YukonXML.com, I found the problem. To access an existing database, I need to use code like this rather than what I had:
Database SelectedDatabase = SelectedServer.Databases[comboDB.Text];
That is, use the Databases collection of the Server object. What's weird is that I'll swear that the code I had was working in earlier versions of the SQL Server 2005 CTP. So either I'm on a bad dose of cold medicines or my version worked before.
Thanks, darshan!!!