c \ # 백업 복원 sql 데이터베이스

1) 프로필
<!--       -->
<add key="server" value="DG-CAOZHENHUA/S2005"/>
<!--    -->
<add key="databases" value="school1;school2;"/>
<!--            -->
<add key="databaseDirPath" value="C:/"/>

프로필 코드 가 져 오기
private string databaseDirPath = ConfigurationManager.AppSettings["databaseDirPath"].ToString();//          
private string server = ConfigurationManager.AppSettings["server"].ToString();//       
private string databases = ConfigurationManager.AppSettings["databases"].ToString();//    

2) 백업 데이터베이스
/// <summary>
        ///      
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBackupDB_Click(object sender, EventArgs e)
        {
            string[] dbs = databases.Split(';');
            foreach (string db in dbs)
            {
                if (db != "")
                {
                    try
                    {
                        string backupDBPath = databaseDirPath + db+".bak";//       
                        if (DelExistFile(backupDBPath))//           
                        {
                            string connection = string.Format("SERVER='{0}';DATABASE=master; TRUSTED_CONNECTION=true;", server);//       
                            string sql = string.Format("Backup database {0} to disk = '{1}'", db, backupDBPath); //       
                            SqlConnection conn = new SqlConnection(connection);
                            conn.Open();
                            SqlCommand command = new SqlCommand(sql, conn);
                            command.ExecuteNonQuery();
                            Log.WriteLog(db + "     !");
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteLog(ex.ToString());
                    }
                }
            }
        }
 /// <summary>
        ///               
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private bool DelExistFile(string filePath)
        {
            bool flag = false;
            try
            {
                //            
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                    Log.WriteLog(filePath + "    !");
                }
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
            }
            return flag;
        }
    }

3) 데이터베이스 복원
 
/// <summary>
        ///      
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRestoreDB_Click(object sender, EventArgs e)
        {
            string[] dbs = databases.Split(';');
            foreach (string db in dbs)
            {
                string backupDBPath = databaseDirPath + db + ".bak";//       
                if (db != "")
                {
                    try
                    {
                        string connection = string.Format("SERVER='{0}';DATABASE=master; TRUSTED_CONNECTION=true;", server);//       
                        string sql = string.Format("restore database {0} from disk ='{1}' with replace", db, backupDBPath);
                        SqlConnection conn = new SqlConnection(connection);
                        conn.Open();
                        SqlCommand command = new SqlCommand(sql, conn);
                        command.ExecuteNonQuery();
                        Log.WriteLog(db + "     !");
                    }
                    catch (Exception ex)
                    {
                        Log.WriteLog(ex.ToString());
                    }
                }
            }
        }

좋은 웹페이지 즐겨찾기