欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年06月30日 11:46:53   作者:a771948524  
這篇文章主要介紹了C#實現(xiàn)的SQL備份與還原功能,結(jié)合具體實例形式分析了C#操作數(shù)據(jù)庫實現(xiàn)SQL備份與還原相關(guān)的控件、SQL連接、文件等操作技巧,需要的朋友可以參考下

本文實例講述了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ù)備份失??!", "提示框", 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#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《C#窗體操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)

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

相關(guān)文章

  • C#圓形頭像框制作并從數(shù)據(jù)庫讀取

    C#圓形頭像框制作并從數(shù)據(jù)庫讀取

    本文主要介紹了C#圓形頭像框制作并從數(shù)據(jù)庫讀取,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • C#中Request.Cookies 和 Response.Cookies 的區(qū)別分析

    C#中Request.Cookies 和 Response.Cookies 的區(qū)別分析

    本文通過實例代碼向我們展示了C#中Request.Cookies 和 Response.Cookies 的區(qū)別,文章淺顯易懂,這里推薦給大家。
    2014-11-11
  • C#中環(huán)境變量示例詳解

    C#中環(huán)境變量示例詳解

    環(huán)境變量是操作系統(tǒng)中存儲的一種機制,用于保存與操作系統(tǒng)環(huán)境和應(yīng)用程序運行相關(guān)的配置信息,在 C# 中,可以使用 Environment.GetEnvironmentVariable 方法來獲取特定環(huán)境變量的值,下面給大家介紹C#中環(huán)境變量示例代碼,一起看看吧
    2024-05-05
  • C語言使用getch()讀取方向鍵

    C語言使用getch()讀取方向鍵

    getch()是編程中所用的函數(shù),這個函數(shù)是一個不回顯函數(shù),當用戶按下某個字符時,函數(shù)自動讀取,無需按回車,有的C語言命令行程序會用到此函數(shù)做游戲,但是這個函數(shù)并非標準函數(shù),要注意移植性
    2021-07-07
  • C# 字符串、數(shù)組和List的截取和轉(zhuǎn)換實例

    C# 字符串、數(shù)組和List的截取和轉(zhuǎn)換實例

    下面小編就為大家分享一篇C# 字符串、數(shù)組和List的截取和轉(zhuǎn)換實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • C#和SQL實現(xiàn)的字符串相似度計算代碼分享

    C#和SQL實現(xiàn)的字符串相似度計算代碼分享

    這篇文章主要介紹了C#和SQL實現(xiàn)的字符串相似度計算代碼分享,本文分別給出了C#語言和SQL語言的實現(xiàn)代碼,需要的朋友可以參考下
    2014-10-10
  • C#基本語法簡介

    C#基本語法簡介

    本文詳細講解了C#的基本語法,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2022-04-04
  • DirectInfo.GetFiles返回數(shù)組的默認排序示例

    DirectInfo.GetFiles返回數(shù)組的默認排序示例

    這篇文章主要介紹了,DirectInfo.GetFiles返回數(shù)組的默認排序示例NTFS和CDFS下,是按照字母順序,而FAT下,按照文件創(chuàng)建時間順序
    2014-01-01
  • c# 引用Nlog插件的步驟

    c# 引用Nlog插件的步驟

    這篇文章主要介紹了c# 引用Nlog插件的步驟,幫助大家更好的理解和學(xué)習使用c#,感興趣的朋友可以了解下
    2021-04-04
  • C#標識符的使用小結(jié)

    C#標識符的使用小結(jié)

    C#標識符還是比較常見的東西,這里我們主要介紹C#標識符中的用法,包括介紹 static 的方法和bool 的形參等方面
    2014-01-01

最新評論