欧美日韩一区二区三区四区不卡,日韩欧美视频一区二区三区四区,久久精品欧美一区二区三区不卡,国产精品久久久乱弄

咨詢電話:186 7916 6165 咨詢電話:186 7916 6165 (微信同號)    在線QQ:181796286
NEWS BLOG ·
學無止境
關注開優網絡 關注前沿
ASP.NET TextBox TextMode為Password從數據庫綁定不能
LitJson字符串解析

ASP.NET中使用代碼來進行備份和還原數據庫

發表日期:2016-07-31    文章編輯:南昌開優網絡    瀏覽次數:4257    標簽:ASP.NET應用

Sql代碼

//備份數據庫  
backup database db_CSManage to disk='c:\backup.bak'  
//還原數據庫,必須先備份該數據庫的日志文件到原先的備份文件中  
backup log db_CSManage to disk='c:\backup.bak'  
restore database db_CSManage from disk='c:\backup.bak'  

 其中db_CSManage是數據庫名稱,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("數據備份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);  
            this.Close();  
        }  
        else  
        {  
            MessageBox.Show("數據備份失敗!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);  
        }  
  
    }  
    else  
    {  
        MessageBox.Show("請填寫備份的正確位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  
    }// end   
}  
catch (Exception ee)  
{  
    MessageBox.Show(ee.Message.ToString());  
}  
 


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

還原數據庫的代碼

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 數據庫的進程--------------  
    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();   //強行關閉用戶進程   
        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();  
    sqlcon.Dispose();  
    MessageBox.Show("數據還原成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  
    MessageBox.Show("為了必免數據丟失,在數據庫還原后將關閉整個系統。");  
    Application.Exit();  
}  
else  
{  
    MessageBox.Show("請選擇備份文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
}  
主站蜘蛛池模板: 梓潼县| 宣城市| 马公市| 花莲市| 灵璧县| 新河县| 永靖县| 元阳县| 新昌县| 兴海县| 老河口市| 宿松县| 景洪市| 太仆寺旗| 甘洛县| 黑山县| 老河口市| 泸定县| 苏尼特左旗| 通道| 剑河县| 乡宁县| 开鲁县| 托克托县| 通河县| 长乐市| 阜城县| 岢岚县| 南投市| 会东县| 柘荣县| 新民市| 壶关县| 谢通门县| 德阳市| 慈溪市| 司法| 苍梧县| 梁山县| 海原县| 唐河县|