国产一区黄,国产夫妻一区,免费在线观看黄色小视频,日本黄色免费在线,久亚洲精品不子伦一区,日99久9在线免费,亚洲欧美日韩国产精品b站在线看

主營業(yè)務(wù):網(wǎng)站制作 網(wǎng)站優(yōu)化 小程序制作 石家莊網(wǎng)站制作網(wǎng)站建設(shè)有限公司歡迎您!
石家莊閃云網(wǎng)絡(luò)科技有限公司
客服熱線15383239821
如何讓自己的網(wǎng)站更有質(zhì)量?

ASP.NET中使用代碼來進行備份和還原數(shù)據(jù)庫

發(fā)布時間:2010/10/23 18:41:24

Sql代碼
-- 備份數(shù)據(jù)庫   
backup database db_CSManage to disk="c:\backup.bak"  
-- 還原數(shù)據(jù)庫,必須先備份該數(shù)據(jù)庫的日志文件到原先的備份文件中   
backup log db_CSManage to disk="c:\backup.bak"  
restore database db_CSManage from disk="c:\backup.bak"  
-- 備份數(shù)據(jù)庫
backup database db_CSManage to disk="c:\backup.bak"
-- 還原數(shù)據(jù)庫,必須先備份該數(shù)據(jù)庫的日志文件到原先的備份文件中
backup log db_CSManage to disk="c:\backup.bak"
restore database db_CSManage from disk="c:\backup.bak"

    其中db_CSManage是數(shù)據(jù)庫名稱,disk后的路徑即是備份文件存儲的路徑。
知道了SQL語句,那么在.NET代碼中就容易多了,備份的.NET代碼如下:

C#代碼
try  
{   
    if (txtPath.Text != "" && txtName122.Text != "")   
     {   
         getSqlConnection geCon = new getSqlConnection();   
         SqlConnection con = geCon.GetCon();   
  
        string strBacl = "backup database db_CSManage to disk="" + txtPath.Text.Trim() + "\\" + txtName.Text.Trim() + ".bak"";   
         SqlCommand Cmd = new SqlCommand(strBacl, con);   
        if (Cmd.ExecuteNonQuery() != 0)   
         {   
             MessageBox.Show("數(shù)據(jù)備份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);   
            this.Close();   
         }   
        else  
         {   
             MessageBox.Show("數(shù)據(jù)備份失??!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);   
         }   
  
     }   
    else  
     {   
         MessageBox.Show("請?zhí)顚憘浞莸恼_位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);   
  
     }// end   
}   
catch (Exception ee)   
{   
     MessageBox.Show(ee.Message.ToString());   
}  
try
{
 if (txtPath.Text != "" && txtName122.Text != "")
 {
  getSqlConnection geCon = new getSqlConnection();
  SqlConnection con = geCon.GetCon();

  string strBacl = "backup database db_CSManage to disk="" + txtPath.Text.Trim() + "\\" + txtName.Text.Trim() + ".bak"";
  SqlCommand Cmd = new SqlCommand(strBacl, con);
  if (Cmd.ExecuteNonQuery() != 0)
  {
   MessageBox.Show("數(shù)據(jù)備份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
   this.Close();
  }
  else
  {
   MessageBox.Show("數(shù)據(jù)備份失敗!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
  }

 }
 else
 {
  MessageBox.Show("請?zhí)顚憘浞莸恼_位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);

 }// end
}
catch (Exception ee)
{
 MessageBox.Show(ee.Message.ToString());
}

以下是還原數(shù)據(jù)庫的代碼,在示例中發(fā)現(xiàn)開頭得先刪除與該數(shù)據(jù)庫相關(guān)的進程,然后在還原之前得先把數(shù)據(jù)庫日志備份到開始的備份文件中,然后才還原備份文件,要不然會出錯的,代碼如下:

C#代碼
if (textPaht.Text != "")   
{   
     getSqlConnection geCon = new getSqlConnection();   
     SqlConnection con = geCon.GetCon();   
    if (con.State == ConnectionState.Open)   
     {   
         con.Close();   
     }   
    string DateStr = "Data Source=niunan\\sqlexpress;Database=master;User id=sa;PWD=123456";   
     SqlConnection conn = new SqlConnection(DateStr);   
     conn.Open();   
  
    //-------------------殺掉所有連接 db_CSManage 數(shù)據(jù)庫的進程--------------   
    string strSQL = "select spid from master..sysprocesses where dbid=db_id( "db_CSManage") ";   
     SqlDataAdapter Da = new SqlDataAdapter(strSQL, conn);   
  
     DataTable spidTable = new DataTable();   
     Da.Fill(spidTable);   
  
     SqlCommand Cmd = new SqlCommand();   
     Cmd.CommandType = CommandType.Text;   
     Cmd.Connection = conn;   
  
    for (int iRow = 0; iRow <= spidTable.Rows.Count - 1; iRow++)   
     {   
         Cmd.CommandText = "kill " + spidTable.Rows[iRow][0].ToString();   //強行關(guān)閉用戶進程   
         Cmd.ExecuteNonQuery();   
     }   
     conn.Close();   
     conn.Dispose();   
    //--------------------------------------------------------------------   
  
     SqlConnection sqlcon = new SqlConnection(DateStr);   
     sqlcon.Open();   
    string sql = "backup log db_CSManage to disk="" + textPaht.Text.Trim() + "" restore database db_CSManage from disk="" + textPaht.Text.Trim() + """;   
     SqlCommand sqlCmd = new SqlCommand(sql, sqlcon);   
     sqlCmd.ExecuteNonQuery();   
     sqlCmd.Dispose();   
     sqlcon.Close();

上一篇: cook 的設(shè)置
下一篇: 常用的存儲過程

相關(guān)新聞推薦

在線客服 : 服務(wù)熱線:15383239821 電子郵箱: 27535611@qq.com

公司地址:石家莊市新華區(qū)九中街江西大廈4062

備案號:冀ICP備2022000585號-4