調(diào)用sql語句實(shí)現(xiàn)SqlServer的備份和還原

字號(hào):


    調(diào)用sql語句實(shí)現(xiàn)SqlServer的備份還原,包括完整備份和差異備份,因?yàn)閳?zhí)行備份還原需要一定的時(shí)間,因此需要設(shè)定 CommandTimeout參數(shù)。
    /// <summary>
    /// 備份數(shù)據(jù)庫 調(diào)用SQL語句
    /// </summary>
    /// <param name="strFileName">備份文件名</param>
    /// <param name="BackUpType">0表示完整備份,為1表示差異備份</param>
    /// <returns></returns>
    public bool BackUPDB(string strFileName, int BackUpType)
    {
    //如果是差異備份,就是看一下文件是否存在,如果不存在,就不執(zhí)行
    if (BackUpType == 1 && File.Exists(strFileName) == false)
    {
    return false;
    }
    bool result = false;
    try
    {
    string[] strConnSqlArr = strConnSql.Split(';');
    string DBName = strConnSqlArr[4].ToString()。Split('=')[1].ToString();//數(shù)據(jù)庫名稱
    string backUp_full = string.Format("backup database {0} to disk = '{1}' ;", DBName, strFileName);
    string backUp_Diff = string.Format("backup database {0} to disk='{1}' WITH DIFFERENTIAL ;", DBName, strFileName);
    WKK.DBUtility.DbHelperSQL.ExecuteSql(BackUpType == 0 ? backUp_full : backUp_Diff, 600);
    result = true;
    }
    catch (Exception ex)
    {
    Common.Log.WriteLog(string.Format("備份{0}數(shù)據(jù)庫失敗", BackUpType == 0 ? "完整" : "差異"), ex);
    // System.Diagnostics.Debug.WriteLine(string.Format("備份{0}數(shù)據(jù)庫失敗", BackUpType == 0 ? "完整" : "差異"));
    result = false;
    }
    finally
    {
    if (result == true)
    {
    string str_InfoContent = string.Format("備份{0}數(shù)據(jù)庫成功", BackUpType == 0 ? "完整" : "差異");
    // System.Diagnostics.Debug.WriteLine(str_InfoContent);
    }
    }
    return result;
    }
    /// <summary>
    /// 還原數(shù)據(jù)庫 使用Sql語句
    /// </summary>
    /// <param name="strDbName">數(shù)據(jù)庫名</param>
    /// <param name="strFileName">備份文件名</param>
    public bool RestoreDB(string strDbName, string strFileName)
    {
    bool result = false;
    try
    {
    string strConnSql = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
    string[] strConnSqlArr = strConnSql.Split(';');
    string DBName = strConnSqlArr[4].ToString()。Split('=')[1].ToString();//數(shù)據(jù)庫名稱
    #region 關(guān)閉所有訪問數(shù)據(jù)庫的進(jìn)程,否則會(huì)導(dǎo)致數(shù)據(jù)庫還原失敗 閆二永 17:39 2014/3/19
    string cmdText = String.Format("EXEC sp_KillThread @dbname='{0}'", DBName);
    WKK.DBUtility.DbHelperSQL.connectionString = strConnSql.Replace(DBName, "master");
    WKK.DBUtility.DbHelperSQL.ExecuteSql(cmdText);
    #endregion
    string Restore = string.Format("RESTORE DATABASE {0} FROM DISK='{1}'WITH replace", DBName, strFileName);
    WKK.DBUtility.DbHelperSQL.ExecuteSql(Restore, 600);
    result = true;
    }
    catch (Exception ex)
    {
    MessageBox.Show("還原數(shù)據(jù)庫失敗rn" + ex.Message, "系統(tǒng)提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    Common.Log.WriteLog(string.Format("還原數(shù)據(jù)庫失敗--{0}", DateTime.Now.ToString()), ex);
    result = false;
    }
    finally
    {
    //恢復(fù)成功后需要重啟程序
    if (result)
    {
    //
    }
    }
    return result;
    }
    /// <summary>
    /// 執(zhí)行一條SQL語句
    /// </summary>
    /// <param name="SQLStringList">sql語句</param>
    /// <param name="SetTimeout"> 等待連接打開的時(shí)間(以秒為單位)。 默認(rèn)值為 15 秒。 </param>
    /// <returns></returns>
    public static int ExecuteSql(string SQLString, int setTimeOut)
    {
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
    using (SqlCommand cmd = new SqlCommand(SQLString, connection))
    {
    try
    {
    connection.Open();
    cmd.CommandTimeout = setTimeOut;
    int rows = cmd.ExecuteNonQuery();
    connection.Close();
    return rows;
    }
    catch (System.Data.SqlClient.SqlException e)
    {
    connection.Close();
    throw e;
    }
    }
    }
    }