SQLSERVER SQL備份還原代碼C#
阿新 • • 發佈:2018-06-15
geb 是否 cep declare ram 出錯 arch path multi
public class BakDBHelper { /// <summary> /// 創建數據庫備份 /// </summary> public string CreateBackup(string dbname, string backname) { string res = ""; //要備份的位置 //string dbfullname = GetDbPath() + string.Format("{0}_{1}.bak", dbname, DateTime.Now.ToString("yyyyMMddhhmmss"));string dbfullname = GetDbPath() + backname; //判斷文件是否存在 if (File.Exists(dbfullname)) { File.Delete(dbfullname); //MessageBox.Show(dbfullname + "的備份文件已經存在,請稍後再試"); } try { SqlConnection con= new SqlConnection(GetConn()); SqlCommand cmd = con.CreateCommand(); con.Open(); try { cmd.CommandText = "use master"; cmd.ExecuteNonQuery(); //1. 執行備份操作 StringBuilder sql = newStringBuilder(); //sql.Append("exec master.dbo.proc_Backup @dbName,@dbFullName"); sql.Append(@"DECLARE @kid varchar(100) SET @kid=‘‘ SELECT @kid=@kid+‘KILL ‘+CAST(spid as Varchar(10)) FROM master..sysprocesses WHERE dbid=DB_ID(@dbName) PRINT @kid EXEC(@kid); backup database "+ dbname + " to disk=@dbFullName;"); SqlParameter[] parameters = new SqlParameter[]{ new SqlParameter("@dbName",SqlDbType.NVarChar,200), new SqlParameter("@dbFullName",SqlDbType.NVarChar,200), }; parameters[0].Value = dbname; parameters[1].Value = dbfullname; cmd.Parameters.Clear(); cmd.Parameters.AddRange(parameters); cmd.CommandText = sql.ToString(); cmd.ExecuteNonQuery(); res = "備份完成"; } finally { con.Close(); } } catch (Exception ex) { res = "創建數據庫備份出錯:" + ex; } return res; } /// <summary> /// 還原數據庫 /// </summary> public string Restore(string dbname, string backname) { string res = ""; //1.獲取還原數據庫和文件 string dbFullName = GetDbPath() + backname; try { //2.執行還原操作 SqlConnection con = new SqlConnection(GetConn()); SqlCommand cmd = con.CreateCommand(); con.Open(); try { cmd.CommandText = "use master"; cmd.ExecuteNonQuery(); StringBuilder sql = new StringBuilder(); //sql.Append("exec proc_Restore @dbFullName,@dbName"); sql.Append(@"--1.1修改為單用模式 exec(N‘ALTER DATABASE ‘+@dbName+‘ SET SINGLE_USER WITH ROLLBACK IMMEDIATE‘); --1.2結束鏈接進程 DECLARE @kid varchar(max) SET @kid=‘‘ SELECT @kid=@kid+‘KILL ‘+CAST(spid as Varchar(10)) FROM master..sysprocesses WHERE dbid=DB_ID(@dbName) ; EXEC(@kid) ; --2.執行還原語句 restore database @dbName from disk=@dbFullName with replace --覆蓋現有的數據庫 --3.重置數據庫為多用戶模式 exec(N‘ALTER DATABASE ‘+@dbName+‘ SET MULTI_USER WITH ROLLBACK IMMEDIATE‘);"); SqlParameter[] parameters = new SqlParameter[]{ new SqlParameter("@dbName",SqlDbType.NVarChar,200), new SqlParameter("@dbFullName",SqlDbType.NVarChar,200), }; parameters[0].Value = dbname; parameters[1].Value = dbFullName; cmd.CommandText = sql.ToString(); cmd.Parameters.AddRange(parameters); cmd.ExecuteNonQuery(); res = "還原數據庫完成"; } finally { con.Close(); } } catch (Exception ex) { res = "還原數據庫出錯" + ex; } return res; } private static string GetDbPath() { return ConfigurationManager.AppSettings["bak_url"].ToString(); } private static string GetConn() { return ConfigurationManager.AppSettings["bak_con"].ToString(); } public int execSQLCommand(string sql) { int res; try { //2.執行還原操作 SqlConnection con = new SqlConnection(GetConn()); SqlCommand cmd = con.CreateCommand(); con.Open(); try { cmd.CommandText = sql; res = cmd.ExecuteNonQuery(); } finally { con.Close(); } } catch (Exception) { return -1; } return res; } }
SQLSERVER SQL備份還原代碼C#