国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#實(shí)現(xiàn)的SQL備份與還原功能示例

2020-01-24 00:35:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了C#實(shí)現(xiàn)的SQL備份與還原功能。分享給大家供大家參考,具體如下:

//記得加 folderBrowserDialog1 openFileDialog1 控件using System.Data.SqlClient; //連接數(shù)據(jù)庫(kù) 公共變量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) //打開(kāi) 備份路徑{if (folderBrowserDialog1.ShowDialog() == DialogResult.OK){txtPath.Text = folderBrowserDialog1.SelectedPath.ToString();}}private void button2_Click(object sender, EventArgs e) //備份名稱(chēng) 保存 {try{if (txtPath.Text != "" ){getSqlConnection geCon = new getSqlConnection();SqlConnection con = geCon.GetCon();string strBacl = "backup database Kc to disk='" + txtPath.Text.Trim() + "http://" + 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("請(qǐng)?zhí)顚?xiě)備份的正確位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);}// end }catch (Exception ee){MessageBox.Show(ee.Message.ToString());}}}}private void button3_Click(object sender, EventArgs e) //打開(kāi) 將要還原的文件{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ù)庫(kù)是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ù)庫(kù)的進(jìn)程--------------// string sql = " SELECT spid FROM master..sysprocesses WHERE dbid=db_id('" + strDBName + "')";string strSQL = "select spid from master..sysprocesses where dbid=db_id( 'Kc') ";//讀取連接當(dāng)前數(shù)據(jù)庫(kù)的進(jìn)程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(); //強(qiáng)行關(guān)閉用戶(hù)進(jì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ù)庫(kù)還原后將關(guān)閉整個(gè)系統(tǒng)。");Application.Exit();}else{MessageBox.Show("請(qǐng)選擇備份文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);}}

更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《C#常見(jiàn)控件用法教程》、《C#窗體操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》及《C#程序設(shè)計(jì)之線(xiàn)程使用技巧總結(jié)

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 通海县| 常宁市| 泗阳县| 宜兴市| 黔西| 鄯善县| 连山| 上栗县| 杂多县| 漠河县| 怀仁县| 岳阳县| 曲松县| 张掖市| 潞城市| 屏南县| 修水县| 新泰市| 海城市| 太仓市| 都兰县| 德安县| 杂多县| 兴宁市| 定远县| 华亭县| 大化| 鞍山市| 仁寿县| 宝清县| 白银市| 蚌埠市| 安溪县| 云霄县| 大同县| 江安县| 叶城县| 柏乡县| 建瓯市| 长子县| 华阴市|