怎么在C#項目中實現(xiàn)SQL備份?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
具體如下:
//記得加 folderBrowserDialog1 openFileDialog1 控件 using System.Data.SqlClient; //連接數(shù)據(jù)庫 公共變量 namespace WindowsApplication1.GoodMenhod { class getSqlConnection { string sql = "Data Source=win7-pc;database=Kc;uid=sa;pwd=sa"; SqlConnection conn; public SqlConnection GetCon() { conn = new SqlConnection(sql); conn.Open(); return conn; } } } using System.Data.SqlClient; using WindowsApplication1.GoodMenhod; //引用命名空間 namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) //打開 備份路徑 { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { txtPath.Text = folderBrowserDialog1.SelectedPath.ToString(); } } private void button2_Click(object sender, EventArgs e) //備份名稱 保存 { try { if (txtPath.Text != "" ) { getSqlConnection geCon = new getSqlConnection(); SqlConnection con = geCon.GetCon(); string strBacl = "backup database Kc 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ù)備份失?。?quot;, "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("請?zhí)顚憘浞莸恼_位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information); }// end } catch (Exception ee) { MessageBox.Show(ee.Message.ToString()); } } } } private void button3_Click(object sender, EventArgs e) //打開 將要還原的文件 { openFileDialog1.FilterIndex = 0; openFileDialog1.FileName = ""; openFileDialog1.Filter = "txt files (*.bak)|*.bak|All files (*.*)|*.*"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { textPaht.Text = openFileDialog1.FileName.ToString(); } } private void button4_Click(object sender, EventArgs e) //還原 { if (textPaht.Text != "") { getSqlConnection geCon = new getSqlConnection(); SqlConnection con = geCon.GetCon(); if (con.State == ConnectionState.Open) { con.Close(); } //連接的數(shù)據(jù)庫是master,所以要初始化新的連接字符串 string DateStr = "Data Source=win7-pc;Database=master;User id=sa;PWD=sa"; SqlConnection conn = new SqlConnection(DateStr); conn.Open(); //-------------------殺掉所有連接 db_CSManage 數(shù)據(jù)庫的進程-------------- // string sql = " SELECT spid FROM master..sysprocesses WHERE dbid=db_id('" + strDBName + "')"; string strSQL = "select spid from master..sysprocesses where dbid=db_id( 'Kc') ";//讀取連接當前數(shù)據(jù)庫的進程 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(); SqlCommand sqlCmd = new SqlCommand("backup database Kc to disk='" + textPaht.Text.Trim() + "' restore database Kc from disk='" + textPaht.Text.Trim() + "'", sqlcon); sqlCmd.ExecuteNonQuery(); sqlCmd.Dispose(); sqlcon.Close(); sqlcon.Dispose(); MessageBox.Show("數(shù)據(jù)還原成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("為了必免數(shù)據(jù)丟失,在數(shù)據(jù)庫還原后將關(guān)閉整個系統(tǒng)。"); Application.Exit(); } else { MessageBox.Show("請選擇備份文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
關(guān)于怎么在C#項目中實現(xiàn)SQL備份問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道了解更多相關(guān)知識。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文名稱:怎么在C#項目中實現(xiàn)SQL備份-創(chuàng)新互聯(lián)
文章源于:http://www.rwnh.cn/article12/hdggc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站導航、網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計公司、全網(wǎng)營銷推廣、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容