C#實現的SQL備份與還原功能示例
更新時間:2017年06月30日 11:46:53 作者:a771948524
這篇文章主要介紹了C#實現的SQL備份與還原功能,結合具體實例形式分析了C#操作數據庫實現SQL備份與還原相關的控件、SQL連接、文件等操作技巧,需要的朋友可以參考下
本文實例講述了C#實現的SQL備份與還原功能。分享給大家供大家參考,具體如下:
//記得加 folderBrowserDialog1 openFileDialog1 控件
using System.Data.SqlClient; //連接數據庫 公共變量
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("數據備份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
else
{
MessageBox.Show("數據備份失??!", "提示框", 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();
}
//連接的數據庫是master,所以要初始化新的連接字符串
string DateStr = "Data Source=win7-pc;Database=master;User id=sa;PWD=sa";
SqlConnection conn = new SqlConnection(DateStr);
conn.Open();
//-------------------殺掉所有連接 db_CSManage 數據庫的進程--------------
// string sql = " SELECT spid FROM master..sysprocesses WHERE dbid=db_id('" + strDBName + "')";
string strSQL = "select spid from master..sysprocesses where dbid=db_id( 'Kc') ";//讀取連接當前數據庫的進程
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();
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("數據還原成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("為了必免數據丟失,在數據庫還原后將關閉整個系統(tǒng)。");
Application.Exit();
}
else
{
MessageBox.Show("請選擇備份文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
更多關于C#相關內容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《C#窗體操作技巧匯總》、《C#數據結構與算法教程》、《C#面向對象程序設計入門教程》及《C#程序設計之線程使用技巧總結》
希望本文所述對大家C#程序設計有所幫助。
相關文章
C#中Dictionary與List的用法區(qū)別以及聯(lián)系詳解
List和Dictionary想必是我們平常用到最多的C#容器了,他們使用起來都很簡單,這篇文章主要給大家介紹了關于C#中Dictionary與List的用法區(qū)別以及聯(lián)系的相關資料,需要的朋友可以參考下2023-11-11
C#?委托與?Lambda?表達式轉換機制及弱事件模式下的生命周期詳解
本文介紹了C#委托和Lambda表達式的工作原理,包括委托的內部結構、Lambda表達式的轉換機制以及弱事件模式下的生命周期管理,感興趣的朋友一起看看吧2025-02-02
C#使用Sleep(Int32)方法實現動態(tài)顯示時間
這篇文章主要為大家詳細介紹了C#如何使用Sleep(Int32)方法實現動態(tài)顯示時間,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下2024-01-01

