欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在C#項(xiàng)目中實(shí)現(xiàn)SQL備份-創(chuàng)新互聯(lián)

怎么在C#項(xiàng)目中實(shí)現(xiàn)SQL備份?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

成都創(chuàng)新互聯(lián)公司主營(yíng)平谷網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app軟件開(kāi)發(fā)公司,平谷h5小程序開(kāi)發(fā)搭建,平谷網(wǎng)站營(yíng)銷(xiāo)推廣歡迎平谷等地區(qū)企業(yè)咨詢(xún)

具體如下:

//記得加 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() + "\\" + 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#項(xiàng)目中實(shí)現(xiàn)SQL備份問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

本文名稱(chēng):怎么在C#項(xiàng)目中實(shí)現(xiàn)SQL備份-創(chuàng)新互聯(lián)
文章源于:http://chinadenli.net/article12/hdggc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器網(wǎng)站導(dǎo)航網(wǎng)站設(shè)計(jì)網(wǎng)頁(yè)設(shè)計(jì)公司全網(wǎng)營(yíng)銷(xiāo)推廣服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷(xiāo)型網(wǎng)站建設(shè)