static public void DumpStructure(string sDatabaseName, string sStructureFilename) { if (!Globals.DatabaseExists(sDatabaseName)) { return; } // If the optional filename is not included, default to the database name. if (sStructureFilename == null) { sStructureFilename = sDatabaseName + " Structure.txt"; } // If the filename does not include the path, default to the DbDir path. if (sStructureFilename.IndexOf('\\') == -1) { sStructureFilename = Globals.DbFolder + sStructureFilename; } // Open connection to database string sConnectionString = "Server=" + Globals.Server + "; Database=" + Globals.CSql(sDatabaseName) + "; User ID = sa;" + "Password=inscrutable; Connect Timeout = 2; Pooling=false;"; using (SqlConnection connection = new SqlConnection(sConnectionString)) { try { connection.Open(); } catch (Exception) { throw new Exception("Unable to open database " + sDatabaseName); } StreamWriter file = File.CreateText(sStructureFilename); WriteQueryResults("Class$", "select * from Class$ order by id", connection, file); WriteQueryResults("Field$", "select * from Field$ order by id", connection, file); WriteQueryResults("ClassPar$", "select * from ClassPar$ order by src, depth", connection, file); SqlCommand command; StringCollection names = new StringCollection(); command = new SqlCommand("select name from sysobjects where type = 'u' order by name", connection); SqlDataReader reader = command.ExecuteReader(); try { while (reader.Read()) { string s = reader.GetString(0); names.Add(s); } } finally { // Always call Close when done reading. reader.Close(); } for (int i = 0; i < names.Count; ++i) { WriteTable(names[i], connection, file); } connection.Close(); file.Flush(); file.Close(); } }
static public void RestoreDB(string sDatabaseName, string sBackupFilename) { // If the optional filename is not included, default to the database name. if (sBackupFilename == null) { sBackupFilename = sDatabaseName + ".bak"; } // If the filename does not include the path, default to the DbDir path. if (sBackupFilename.IndexOf('\\') == -1) { sBackupFilename = Globals.DbFolder + sBackupFilename; } if (!File.Exists(sBackupFilename)) { Console.WriteLine("Cannot restore " + sBackupFilename + " because the backup file does not exist."); return; } using (SqlConnection oConn = Globals.Conn) { // Under some rare situations, the restore fails if the database already exists, even with move // and replace. It may have something to do with duplicate logical names, although that's not // consistent. So to be safe, we'll delete the database first if it is present. string ssql = "If exists (select name from sysdatabases where name = '" + sDatabaseName + "') " + "begin " + "drop database [" + sDatabaseName + "] " + "end"; using (SqlCommand oCommand = new SqlCommand(ssql, oConn)) { oCommand.ExecuteNonQuery(); } // Get the list of the logical files in the backup file and reset the path for each one. ssql = "restore filelistonly from disk = " + Globals.CSqlN(sBackupFilename); using (SqlCommand oCommand = new SqlCommand(ssql, oConn)) using (SqlDataReader oReader = oCommand.ExecuteReader()) { ssql = "restore database [" + sDatabaseName + "] from disk = " + Globals.CSqlN(sBackupFilename) + " with replace"; int iLogFile = 0; while (oReader.Read()) { string sFilename = Globals.DbFolder + sDatabaseName; if (oReader["Type"].ToString().ToUpper() == "D") { sFilename += ".mdf"; } else { string sExt = (oReader["PhysicalName"].ToString().IndexOf("_log") > -1) ? "_log.ldf" : ".ldf"; if (iLogFile++ == 0) { sFilename += "_log.ldf"; } else { sFilename += iLogFile + "_log.ldf"; } } ssql += ", move " + Globals.CSqlN(oReader["LogicalName"].ToString()) + " to " + Globals.CSqlN(sFilename); } } using (SqlCommand oCommand = new SqlCommand(ssql, oConn)) { // Default is 30 which is too short when restoring a big database from SQL Server 2000 // on slower machines. 0 is unlimited time. oCommand.CommandTimeout = 0; oCommand.ExecuteNonQuery(); } } }