This is basically what I'm doing (the code is attached to a button click event, a different button for each database):
//Set up a StringCollection object to hold the paths to the MDF and LDF files
StringCollection files = new StringCollection();
//Add the paths - depending on which database you want loaded
files.Add(@"C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\NBNData_Data.MDF");
files.Add(@"C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\NBNData_log.ldf");
//Set up a connection string
string connectionString = "Server=YourServerName\\SQLExpress;Trusted_Connection=Yes;";
//Set up a connection
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
ServerConnection servConn = new ServerConnection(connection);
//Set up a server object and connect to it
Server sqlServer = new Server(servConn);
//Check if a database is attached first
//If it's attached, detach it
if (sqlServer.Databases.Contains("NBNData"))
{
sqlServer.DetachDatabase("NBNData", false);
sqlServer.KillAllProcesses("NBNData");
sqlServer.Refresh();
}
//Attach the new database files
sqlServer.AttachDatabase("NBNData", files, "YourServerName\\DBOwner",AttachOptions.None);
sqlServer.KillAllProcesses("NBNData");
sqlServer.Refresh();
connection.Close();
connection.Dispose();
I've left out any exception trapping for simplicity and I may have gone unecessarily over the top with closing, disposing and killing.
I have no idea how to set the Object Sheet Path and the Map File Path using this approach.
Any suggestions very welcome.
Alan