public static bool DbBackup(string url) { bool flag; Backup backup = new BackupClass(); SQLServer serverObject = new SQLServerClass(); try { serverObject.LoginSecure = false; serverObject.Connect(ConfigurationManager.AppSettings["Server"], ConfigurationManager.AppSettings["User"], ConfigurationManager.AppSettings["Password"]); backup.Action = SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database; backup.Database = ConfigurationManager.AppSettings["DB"]; backup.Files = url; backup.BackupSetName = ConfigurationManager.AppSettings["DB"]; backup.BackupSetDescription = "数据库备份"; backup.Initialize = true; backup.SQLBackup(serverObject); flag = true; } catch { flag = false; } finally { serverObject.DisConnect(); } return flag; }
public bool DbBackup(string url) { Backup oBackup = new BackupClass(); SQLServer oSQLServer = new SQLServerClass(); bool result; try { oSQLServer.LoginSecure = false; oSQLServer.Connect(this.server, this.uid, this.pwd); oBackup.Action = SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database; oBackup.Database = this.database; oBackup.Files = url; oBackup.BackupSetName = this.database; oBackup.BackupSetDescription = "数据库备份"; oBackup.Initialize = true; oBackup.SQLBackup(oSQLServer); result = true; } catch { result = false; } finally { oSQLServer.DisConnect(); } return(result); }
public bool CheckScriptGrammar(string sSql, out string sMessage) { bool flag2; sMessage = string.Empty; SQLServer server = new SQLServerClass(); bool flag = false; try { server.Connect(this.Server, this.UserID, this.Password); flag = true; server.ExecuteImmediate(string.Format("SET PARSEONLY ON;{0}", sSql), SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null); flag2 = true; } catch (Exception exception) { sMessage = exception.Message; flag2 = false; } finally { if (flag) { server.ExecuteImmediate("SET PARSEONLY OFF; ", SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null); server.DisConnect(); } } return(flag2); }
public bool DbRestore(string url) { bool result; if (!this.exepro()) { result = false; } else { Restore oRestore = new RestoreClass(); SQLServer oSQLServer = new SQLServerClass(); try { oSQLServer.LoginSecure = false; oSQLServer.Connect(this.server, this.uid, this.pwd); oRestore.Action = SQLDMO_RESTORE_TYPE.SQLDMORestore_Database; oRestore.Database = this.database; oRestore.Files = url; oRestore.FileNumber = 1; oRestore.ReplaceDatabase = true; oRestore.SQLRestore(oSQLServer); result = true; } catch { result = false; } finally { oSQLServer.DisConnect(); } } return(result); }
public bool BackupDatabase(string sFullName, string sDataBase) { bool flag = false; SQLDMO.Backup backup = new BackupClass(); SQLServer serverObject = new SQLServerClass(); try { serverObject.LoginSecure = false; serverObject.Connect(this.Server, this.UserID, this.Password); backup.Action = SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database; backup.Database = sDataBase; backup.Files = string.Format("[{0}]", sFullName); backup.BackupSetName = sDataBase; backup.BackupSetDescription = string.Format("数据库备份:{0}", sDataBase); backup.Initialize = true; backup.SQLBackup(serverObject); if (System.IO.File.Exists(sFullName)) { flag = true; } } catch (Exception exception) { Log.WriteLog(string.Format("备份数据库[{0}]:BackupDatabase(string sFullName, string sDataBase)", sDataBase), exception.Message); return(flag); } finally { serverObject.DisConnect(); } return(flag); }
public List <string> GetServerDatabases() { List <string> list = new List <string>(); SQLServer server = new SQLServerClass(); try { server.LoginSecure = false; server.Connect(this.Server, this.UserID, this.Password); Databases databases = server.Databases; if (databases.Count > 0) { foreach (SQLDMO.Database database in databases) { list.Add(database.Name.ToLower()); } } } catch (Exception exception) { Log.WriteLog("获取数据库服务器数据库列表:GetServerDatabases()", exception.Message); return(list); } finally { server.DisConnect(); } return(list); }
public static bool DBExists(string sDBName, string sServer, string sUID, string sPWD) { bool flag = false; SQLServer server = new SQLServerClass(); try { server.LoginSecure = false; server.Connect(sServer, sUID, sPWD); Databases databases = server.Databases; if (databases.Count > 0) { foreach (SQLDMO.Database database in databases) { if (sDBName.Trim() == database.Name.Trim()) { return(true); } } return(flag); } } catch (Exception exception) { Log.WriteLog("判断连接的数据库服务器上有无同名数据库:DatabaseExists(string sDBName)", exception.Message); return(flag); } finally { server.DisConnect(); } return(flag); }
public bool DbBackup(string url) { bool result = true; Backup backup = new BackupClass(); SQLServer sQLServer = new SQLServerClass(); try { sQLServer.LoginSecure = false; string[] array = backup_BackupList.pstr.Split(new char[] { ';' }); sQLServer.Connect(array[0].Split(new char[] { '=' })[1], array[3].Split(new char[] { '=' })[1], array[4].Split(new char[] { '=' })[1]); backup.Action = SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database; backup.Database = array[1].Split(new char[] { '=' })[1]; url = url + array[1].Split(new char[] { '=' })[1] + ".bak"; backup.Files = url; backup.BackupSetName = array[1].Split(new char[] { '=' })[1]; backup.BackupSetDescription = DateTime.Now.ToString("yyyyMMddHHmm") + "数据库备份"; backup.Initialize = true; backup.SQLBackup(sQLServer); } catch (Exception) { result = false; throw; } finally { sQLServer.DisConnect(); } return(result); }
public int BatchExecuteSql(List <string> lstDatabases, List <string> lstFiles) { Exception exception; FileManagement management = new FileManagement(); int num = 0; StringBuilder builder = new StringBuilder(); builder.Append(string.Format("/**************************************[{0}]开始批量执行脚本**************************************/\r\n", DateTime.Now.ToString())); SQLServer server = new SQLServerClass(); try { server.Connect(this.Server, this.UserID, this.Password); string command = string.Empty; foreach (string str2 in lstDatabases) { builder.Append(string.Format("\r\n[{0}] 数据库[{1}]准备升级\r\n", DateTime.Now.ToString(), str2)); foreach (string str3 in lstFiles) { try { command = string.Format("USE {0}\r\n{1}", str2, management.ReadFile(str3)); server.ExecuteImmediate(command, SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null); } catch (Exception exception1) { exception = exception1; num++; builder.Append(string.Format("\r\n{0}、[{1}]:{2} 脚本文件异常\r\n异常信息为:{3}\r\n", new object[] { num, DateTime.Now.ToString(), str3, exception.Message })); } } builder.Append(string.Format("\r\n[{0}] 数据库[{1}]升级完毕\r\n", DateTime.Now.ToString(), str2)); } builder.Append(string.Format("\r\n/**************************************[{0}]结束批量执行脚本**************************************/\r\n\r\n", DateTime.Now.ToString())); Log.WriteLog(builder.ToString()); } catch (Exception exception2) { exception = exception2; num = -1; Log.WriteLog("批量执行数据库升级脚本:BatchExecuteSql(List<string> lstDatabases, List<string> lstFiles) ", exception.Message); return(num); } finally { server.DisConnect(); GC.Collect(); } return(num); }
public void ExecuteSql(string sSql) { SQLServer server = new SQLServerClass(); try { server.Connect(this.Server, this.UserID, this.Password); server.ExecuteImmediate(sSql, SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null); } catch (Exception exception) { Log.WriteLog("执行脚本:ExecuteSql(string sSql)", exception.Message); } finally { server.DisConnect(); } }
public bool Connect() { bool flag; SQLServer server = new SQLServerClass(); try { server.LoginSecure = false; server.Connect(this.Server, this.UserID, this.Password); flag = true; } catch (Exception exception) { Log.WriteLog(string.Format("连接数据库服务器[{0}]:Connect()", this.Server), exception.Message); flag = false; } finally { server.DisConnect(); } return(flag); }
public string BackUpDatabase(string backuppath, string ServerName, string UserName, string Password, string strDbName, string strFileName) { SQLServer svr = new SQLServerClass(); try { svr.Connect(ServerName, UserName, Password); Backup bak = new BackupClass(); bak.Action = 0; bak.Initialize = true; bak.Files = backuppath + strFileName + ".config"; bak.Database = strDbName; bak.SQLBackup(svr); return string.Empty; } catch(Exception ex) { string message = ex.Message.Replace("'", " "); message = message.Replace("\n", " "); message = message.Replace("\\", "/"); return message; } finally { svr.DisConnect(); } }
public string RestoreDatabase(string backuppath, string ServerName, string UserName, string Password, string strDbName, string strFileName) { #region 数据库的恢复的代码 SQLServer svr = new SQLServerClass(); try { svr.Connect(ServerName, UserName, Password); QueryResults qr = svr.EnumProcesses(-1); int iColPIDNum = -1; int iColDbName = -1; for (int i = 1; i <= qr.Columns; i++) { string strName = qr.get_ColumnName(i); if (strName.ToUpper().Trim() == "SPID") { iColPIDNum = i; } else if (strName.ToUpper().Trim() == "DBNAME") { iColDbName = i; } if (iColPIDNum != -1 && iColDbName != -1) break; } for (int i = 1; i <= qr.Rows; i++) { int lPID = qr.GetColumnLong(i, iColPIDNum); string strDBName = qr.GetColumnString(i, iColDbName); if (strDBName.ToUpper() == strDbName.ToUpper()) svr.KillProcess(lPID); } Restore res = new RestoreClass(); res.Action = 0; string path = backuppath + strFileName + ".config"; res.Files = path; res.Database = strDbName; res.ReplaceDatabase = true; res.SQLRestore(svr); return string.Empty; } catch (Exception err) { string message = err.Message.Replace("'", " "); message = message.Replace("\n", " "); message = message.Replace("\\", "/"); return message; } finally { svr.DisConnect(); } #endregion }
public int BatchAnalysisScript(List <string> lstFiles, bool blnCheckDataBase, bool blnYesOrNo) { Exception exception; FileManagement management = new FileManagement(); string sSql = string.Empty; int num = 0; StringBuilder builder = new StringBuilder(); builder.Append(string.Format("/**************************************[{0}]开始批量分析脚本**************************************/\r\n", DateTime.Now.ToString())); SQLServer server = new SQLServerClass(); try { server.Connect(this.Server, this.UserID, this.Password); foreach (string str2 in lstFiles) { if (Path.GetExtension(str2) != ".sql") { num++; builder.Append(string.Format("\r\n{0}、[{1}]:{2} 非脚本文件\r\n", num, DateTime.Now.ToString(), str2)); } else { try { sSql = management.ReadFile(str2); if (blnCheckDataBase) { if (blnYesOrNo) { if (this.CheckUseDatabase(sSql)) { num++; builder.Append(string.Format("\r\n{0}、[{1}] 脚本文件“{2}”指定了数据库名\r\n", num, DateTime.Now.ToString(), str2)); } } else if (!this.CheckUseDatabase(sSql)) { num++; builder.Append(string.Format("\r\n{0}、[{1}] 脚本文件“{2}”未指定数据库名\r\n", num, DateTime.Now.ToString(), str2)); } } server.ExecuteImmediate(string.Format("SET PARSEONLY ON;{0}", sSql), SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null); } catch (Exception exception1) { exception = exception1; num++; builder.Append(string.Format("\r\n{0}、[{1}]:{2} 脚本文件异常\r\n异常信息为:{3}\r\n", new object[] { num, DateTime.Now.ToString(), str2, exception.Message })); } finally { server.ExecuteImmediate("SET PARSEONLY OFF; ", SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null); } } } builder.Append(string.Format("\r\n/**************************************[{0}]结束批量分析脚本**************************************/\r\n\r\n", DateTime.Now.ToString())); Log.WriteLog(builder.ToString()); } catch (Exception exception2) { exception = exception2; num = -1; Log.WriteLog("批量分析脚本:BatchAnalysisScript(string lstFiles, bool blnCheckDataBase, bool blnYesOrNo)", exception.Message); return(num); } finally { server.DisConnect(); GC.Collect(); } return(num); }
public bool StoreDatabase(string sFile, string sPath, string sDataBase) { bool flag = false; SQLServer serverObject = new SQLServerClass(); SQLDMO.Restore restore = new RestoreClass(); try { int num3; serverObject.LoginSecure = false; serverObject.Connect(this.Server, this.UserID, this.Password); QueryResults results = serverObject.EnumProcesses(-1); int column = -1; int num2 = -1; for (num3 = 1; num3 < results.Columns; num3++) { string str = results.get_ColumnName(num3); if (str.ToUpper().Trim() == "SPID") { column = num3; } else if (str.ToUpper().Trim() == "DBNAME") { num2 = num3; } if ((column != -1) && (num2 != -1)) { break; } } num3 = 1; while (num3 < results.Rows) { int columnLong = results.GetColumnLong(num3, column); if (results.GetColumnString(num3, num2).ToUpper() == sDataBase.ToUpper()) { serverObject.KillProcess(columnLong); } num3++; } QueryResults results2 = serverObject.ExecuteWithResults(string.Format("RESTORE FILELISTONLY FROM DISK = '{0}'", sFile), null); int num5 = 0; int num6 = 0; for (num3 = 1; num3 < results2.Columns; num3++) { if ("LOGICALNAME" == results2.get_ColumnName(num3).ToUpper()) { num5 = num3; } if ("PHYSICALNAME" == results2.get_ColumnName(num3).ToUpper()) { num6 = num3; } if ((num6 != 0) && (num5 != 0)) { break; } } StringBuilder builder = new StringBuilder(); for (num3 = 1; num3 <= results2.Rows; num3++) { builder.Append(string.Format(@"[{0}],[{1}\{2}]", results2.GetColumnString(num3, num5), sPath, Path.GetFileName(results2.GetColumnString(num3, num6)))); if (num3 != results2.Rows) { builder.Append(","); } } restore.Action = SQLDMO_RESTORE_TYPE.SQLDMORestore_Database; restore.Database = sDataBase; restore.Files = sFile; restore.FileNumber = 1; restore.ReplaceDatabase = false; restore.RelocateFiles = builder.ToString(); restore.SQLRestore(serverObject); Databases databases = serverObject.Databases; foreach (SQLDMO.Database database in databases) { if (database.Name == sDataBase) { return(true); } } return(flag); } catch (Exception exception) { Log.WriteLog(string.Format("还原数据库[{0}]:StoreDatabase(string sFile,string sPath,string sDataBase)", sDataBase), exception.Message); return(flag); } finally { serverObject.DisConnect(); } return(flag); }
/// <summary> /// Populate the databases dropdown list with the DBs found on the specified server /// </summary> private void PopulateDatabaseNamesCmb() { ArrayList dbNames = new ArrayList(); SQLServer sqlInstance = new SQLServerClass(); try { if (_connStr.TrustedConnection) { sqlInstance.LoginSecure=true; sqlInstance.Connect(_connStr.Server,null,null); } else sqlInstance.Connect(_connStr.Server,_connStr.UserId,_connStr.Password); foreach(Database db in sqlInstance.Databases) { dbNames.Add(db.Name); } databaseName.DataSource=dbNames; databaseName.DataBind(); if (databaseName.Items.FindByValue(_connStr.Database)!=null) databaseName.SelectedValue=_connStr.Database; } catch(Exception) { connResult.Text="Error retrieving database list"; databaseName.Items.Clear(); databaseName.Items.Add(new ListItem("-- Error retrieving database list --","0")); } finally { sqlInstance.DisConnect(); } }
// Token: 0x0600027B RID: 635 RVA: 0x00008AA4 File Offset: 0x00006CA4 public static string BackUpDatabase() { string mapPath = FPUtils.GetMapPath(WebConfig.WebPath + "backup/datas"); if (!Directory.Exists(mapPath)) { Directory.CreateDirectory(mapPath); } DbConfigInfo dbConfig = DbConfigs.GetDbConfig(); string str = dbConfig.dbname + "_" + DateTime.Now.ToString("yyyyMMddHHmmss"); if (dbConfig.dbtype == FangPage.Data.DbType.SqlServer) { SQLServer sqlserver = new SQLServerClass(); sqlserver.Connect(dbConfig.dbpath, dbConfig.userid, dbConfig.password); try { if (File.Exists(mapPath + "\\" + dbConfig.dbname + ".bak")) { File.Delete(mapPath + "\\" + dbConfig.dbname + ".bak"); } ((_Backup) new BackupClass { Action = SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database, Initialize = true, Files = mapPath + "\\" + dbConfig.dbname + ".bak", Database = dbConfig.dbname }).SQLBackup(sqlserver); if (File.Exists(mapPath + "\\" + str + ".config")) { using (FPZip fpzip = new FPZip()) { fpzip.AddFile(mapPath + "\\" + dbConfig.dbname + ".bak", dbConfig.dbname + ".bak"); fpzip.ZipSave(mapPath + "\\" + str + ".config"); } File.Delete(mapPath + "\\" + dbConfig.dbname + ".bak"); } return(string.Empty); } catch (Exception ex) { string text = ex.Message.Replace("'", " "); text = text.Replace("\n", " "); return(text.Replace("\\", "/")); } finally { sqlserver.DisConnect(); } } string result; try { if (File.Exists(mapPath + "\\" + str + ".config")) { File.Delete(mapPath + "\\" + str + ".zip"); using (FPZip fpzip = new FPZip()) { fpzip.AddFile(WebConfig.WebPath + dbConfig.dbpath, dbConfig.dbpath); fpzip.ZipSave(mapPath + "\\" + str + ".config"); } } result = string.Empty; } catch (Exception ex) { string text = ex.Message.Replace("'", " "); text = text.Replace("\n", " "); text = text.Replace("\\", "/"); result = text; } return(result); }
// Token: 0x0600027C RID: 636 RVA: 0x00008E34 File Offset: 0x00007034 public static string RestoreDatabase(string backupfile) { string result; if (!File.Exists(backupfile)) { result = "备份文件已不存在。"; } else { DbConfigInfo dbConfig = DbConfigs.GetDbConfig(); if (dbConfig.dbtype == FangPage.Data.DbType.SqlServer) { SQLServer sqlserver = new SQLServerClass(); if (Path.GetExtension(backupfile) == ".zip" || Path.GetExtension(backupfile) == ".config") { FPZip.UnZipFile(backupfile, ""); } backupfile = string.Concat(new string[] { Path.GetDirectoryName(backupfile), "\\", Path.GetFileName(backupfile), "\\", dbConfig.dbname, ".bak" }); if (!File.Exists(backupfile)) { return("备份文件已不存在。"); } try { sqlserver.Connect(dbConfig.dbpath, dbConfig.userid, dbConfig.password); QueryResults queryResults = sqlserver.EnumProcesses(-1); int num = -1; int num2 = -1; for (int i = 1; i <= queryResults.Columns; i++) { string text = queryResults.get_ColumnName(i); if (text.ToUpper().Trim() == "SPID") { num = i; } else if (text.ToUpper().Trim() == "DBNAME") { num2 = i; } if (num != -1 && num2 != -1) { break; } } for (int i = 1; i <= queryResults.Rows; i++) { int columnLong = queryResults.GetColumnLong(i, num); string columnString = queryResults.GetColumnString(i, num2); if (columnString.ToUpper() == dbConfig.dbname.ToUpper()) { sqlserver.KillProcess(columnLong); } } ((_Restore) new RestoreClass { Action = SQLDMO_RESTORE_TYPE.SQLDMORestore_Database, Files = backupfile, Database = dbConfig.dbname, ReplaceDatabase = true }).SQLRestore(sqlserver); File.Delete(backupfile); return(string.Empty); } catch (Exception ex) { return(ex.Message); } finally { sqlserver.DisConnect(); } } string mapPath = FPUtils.GetMapPath(WebConfig.WebPath + Path.GetDirectoryName(dbConfig.dbpath)); try { if (!Directory.Exists(mapPath)) { Directory.CreateDirectory(mapPath); } if (Path.GetExtension(backupfile) == ".zip" || Path.GetExtension(backupfile) == ".config") { FPZip.UnZipFile(backupfile, mapPath); } else { File.Copy(backupfile, mapPath + "\\" + dbConfig.dbname); } result = string.Empty; } catch (Exception ex) { result = ex.Message; } } return(result); }