references to erased database in database collection (via SMO) RRS feed

  • Question

  • After some experiments during which I copied and erased in various locations the same database (and established connection to them) I tried the following code chunk:


    using Microsoft.SqlServer.Management.Smo;

    using Microsoft.SqlServer.Management.Common;


    string ExeDir = "", DataDir="";

    SqlConnection con = new SqlConnection();


    //it seems that VS doesn't like relative path (where am I wrong?) so I transform it to a full absolute one

    ExeDir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

    DataDir = System.IO.Path.GetFullPath(ExeDir + "\\..\\Super.mdf");

    con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + DataDir +";Integrated Security=True;User Instance=True";


    ServerConnection ServConn = new ServerConnection(con);

    Server MyServ = new Server(ServConn);


    At this point what I can't figure out is

    a) why there are references to previously erased database in the collection MyServ.Databases


    Note: the only valid entry in collection is at index 3


    b) why I can refer to the dabases via their index but not via their names:

         MyServ.Databases[3]  OK 

         MyServ.Databases["Super"]   NO!    I obtain a null reference


    Thank you



    Monday, July 9, 2007 6:20 PM