public static bool restoreDB(string databaseName) {//还原数据库 bool retuValue = true; string resultTxt = DateTime.Now.ToString() + ":<br />还原数据库成功,<br />还原的数据库为:" + databaseName; OleDbConnection strConn = createcon(); string tempPath = strConn.ConnectionString.Substring(strConn.ConnectionString.IndexOf("Data Source=") + 12, strConn.ConnectionString.LastIndexOf("\\") - strConn.ConnectionString.IndexOf("Data Source=") - 12); string dbName = strConn.ConnectionString.Substring(strConn.ConnectionString.LastIndexOf("\\")); if (System.IO.File.Exists(tempPath + "\\" + databaseName)) { try { System.IO.File.Copy(tempPath + "\\" + databaseName, tempPath + "\\" + dbName, true); } catch (Exception exp) { resultTxt = DateTime.Now.ToString() + ";<br />还原数据库时出错,<br />原因:" + exp.Message.ToString(); retuValue = false; } } else { resultTxt = DateTime.Now.ToString() + ";<br />还原数据库时出错,原因,<br />相应的数据库没有找到,本错误信息来自页面: App_Code/DB.cs"; retuValue = false; } systemRecord.insertOneRecord("数据库被还原。"); FileOper.writeOneFile("App_Data\\", "restoreDB.txt", resultTxt); return(retuValue); }
public static bool delBackupDB(string databaseName) {//删除备份数据库 bool retuValue = true; string resultTxt = DateTime.Now.ToString() + ":<br />删除数据库成功,<br />被删除的数据库为:" + databaseName; OleDbConnection strConn = createcon(); string tempPath = strConn.ConnectionString.Substring(strConn.ConnectionString.IndexOf("Data Source=") + 12, strConn.ConnectionString.LastIndexOf("\\") - strConn.ConnectionString.IndexOf("Data Source=") - 12); if (System.IO.File.Exists(tempPath + "\\" + databaseName)) { try { System.IO.File.Delete(tempPath + "\\" + databaseName); } catch (Exception exp) { resultTxt = DateTime.Now.ToString() + "<br />删除数据库时出错,原因:<br />" + exp.ToString(); retuValue = false; } } else { resultTxt = DateTime.Now.ToString() + ":<br />删除数据库时出错,原因:<br />相应的数据库没有找到!"; retuValue = false; } systemRecord.insertOneRecord("备份数据库被删除。"); FileOper.writeOneFile("App_Data\\", "delBackupDB.txt", resultTxt); return(retuValue); }
public static bool backupDB(string backupName) {//备份数据库 bool retuValue = true; string resultTxt = DateTime.Now.ToString() + ":<br />备份数据库成功,<br />备份的数据库为:" + backupName; OleDbConnection strConn = createcon(); string tempPath = strConn.ConnectionString.Substring(strConn.ConnectionString.IndexOf("Data Source=") + 12, strConn.ConnectionString.LastIndexOf("\\") - strConn.ConnectionString.IndexOf("Data Source=") - 12); string dbName = strConn.ConnectionString.Substring(strConn.ConnectionString.LastIndexOf("\\")); try { System.IO.File.Copy(tempPath + "\\" + dbName, tempPath + "\\" + backupName, true); } catch (Exception exp) { resultTxt = DateTime.Now.ToString() + ";<br />备份数据库时出错,<br />原因:" + exp.Message.ToString(); retuValue = false; } systemRecord.insertOneRecord("数据库被备份。"); FileOper.writeOneFile("App_Data\\", "backupDB.txt", resultTxt); return(retuValue); }
public static bool compactDB() {//压缩数据库 bool retuValue = true; string resultTxt = DateTime.Now.ToString() + ":<br />压缩数据库失败,<br />请确保没有其它用户连接数据库!"; OleDbConnection strConn = createcon(); object[] oParams; object objJRO = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine")); string tempDb = "CompactDatabase-temp-" + DateTime.Now.ToShortDateString() + ".mdb"; string tempPath = strConn.ConnectionString.Substring(strConn.ConnectionString.IndexOf("Data Source=") + 12, strConn.ConnectionString.LastIndexOf("\\") - strConn.ConnectionString.IndexOf("Data Source=") - 12); string dbName = strConn.ConnectionString.Substring(strConn.ConnectionString.LastIndexOf("\\")); oParams = new object[] { strConn.ConnectionString, strConn.ConnectionString.Substring(0, strConn.ConnectionString.LastIndexOf('\\')) + "\\" + tempDb + ";Jet OLEDB:Engine Type=5" }; try { objJRO.GetType().InvokeMember("CompactDatabase", System.Reflection.BindingFlags.InvokeMethod, null, objJRO, oParams); if (System.IO.File.Exists(tempPath + "\\" + tempDb)) { System.IO.File.Copy(tempPath + "\\" + tempDb, tempPath + "\\" + dbName, true); } } catch (Exception exp) { resultTxt = DateTime.Now.ToString() + ":<br />压缩数据库时出错,<br />原因:" + exp.Message.ToString(); retuValue = false; } System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO); objJRO = null; if (System.IO.File.Exists(tempPath + "\\" + tempDb)) { System.IO.File.Delete(tempPath + "\\" + tempDb); } if (retuValue) { resultTxt = DateTime.Now.ToString() + ":<br />压缩数据库成功!<br />被压缩的为系统数据库."; } systemRecord.insertOneRecord("数据库被压缩。"); FileOper.writeOneFile("App_Data\\", "compactDB.txt", resultTxt); return(retuValue); }