I'm enumerating all databases of an SQL Server 2005 instance using SMO like as shown below and it returns a non existing database (it was existing in the past).
Using conn As SqlConnection = New SqlConnection(_connectionString)
conn.Open()
Dim smoConnection As New ServerConnection(conn)
Dim srv As Server = New Server(smoConnection)
For Each db 开发者_如何学运维As Database In srv.Databases
If db.Name.Contains("blablabla") Then
doStuffOnDatabase(db)
End If
Next
smoConnection.Disconnect()
End Using
On the same server, running
exec sp_databases();
returns the correct database list. I don't understand why SMO does not give me the same list as sp_databases().
The code above works correctly on customer machines. I've only seen it fail like that on two coworkers laptop running under Windows Vista.
Why is SMO returning an old no longer existing database ?
The assembly versions are all 9.0.242.0
Did you drop the old database or just renamed it?
Deleting the per-user master database folder did the trick. It got recreated when restarting the user instance and the trace of the old database disapparead. Looks like something was wrong in there...
The path is
C:\Documents and Settings**USERNAME**\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data**SQL_SERVER_INSTANCE_NAME**
More info about user instances at MSDN
精彩评论