日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#备份数据和还原数据

發(fā)布時間:2023/12/9 C# 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#备份数据和还原数据 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
private void button1_Click(object sender, EventArgs e)
{
//選擇要備份的路徑
this.folderBrowserDialog1.ShowDialog();
this.txtDbBackup.Text = folderBrowserDialog1.SelectedPath;
}

private void btnChooseOK_Click(object sender, EventArgs e)
{

if (txtDbhuanyuan.Text=="")
{
//路徑+數(shù)據(jù)庫名字
string name = txtDbBackup.Text+@"\QMX.mdf";
//備份數(shù)據(jù)庫語句
string sql = string.Format(@"Backup Database QMX To disk='{0}'",name);


if (DBHelpers.Upadate(sql)) {
MessageBox.Show("備份成功");


}
else
{
MessageBox.Show("備份失敗");
}
}
else
{
MessageBox.Show("請選擇路徑");
}



} private void btnDeoxidizeTxt_Click(object sender, EventArgs e)
{
//選擇要恢復(fù)的路徑

string xiaohao;
OpenFileDialog op = new OpenFileDialog();
//默認(rèn)打開D盤
op.InitialDirectory = "D:\\";
op.Filter = "(*.mdf)|*.mdf|(*.png)|*.png|(*.*)|*.*";
op.RestoreDirectory = true;
op.AddExtension = true;
if (op.ShowDialog()==DialogResult.OK)
{
xiaohao= op.FileName;

txtDbhuanyuan.Text = xiaohao;
}
}

private void btnDeoxidize_Click(object sender, EventArgs e)
{
if (txtDbhuanyuan.Text=="")
{
MessageBox.Show("請正確選擇地址");
}
else
{
//恢復(fù)數(shù)據(jù)庫sql語句
string sql = string.Format("if exists(select * from sysdatabases where name='QMX') drop database QMX use master restore database QMX from disk='{0}'", this.txtDbhuanyuan.Text);

if (DBHelpers.Upadate(sql))
{
MessageBox.Show("數(shù)據(jù)庫恢復(fù)成功");
}
else
{
MessageBox.Show("數(shù)據(jù)庫恢復(fù)失敗");
}
}

}

public class DBHelpers {

private static SqlConnection con;

public static SqlConnection Con
{
get
{

if (con == null)
{

con = new SqlConnection(ConfigurationManager.ConnectionStrings["QMXContext"].ConnectionString);
con.Open();
}
else if (con.State == ConnectionState.Broken)
{
con.Close();
con.Open();
}
else if (con.State == ConnectionState.Closed)
{
con.Open();
}



return con;
}

}
public static SqlCommand Cmd
{
get
{
return Con.CreateCommand();
}

}
public static bool Upadate(string sql)
{
SqlCommand cmd = Cmd;
cmd.CommandText = sql;
if (cmd.ExecuteNonQuery()==-1)
{
return true;
}
else
{
return false;
}

}
public static object SelectFromscaler(string sql)
{
SqlCommand cmd = Cmd;
cmd.CommandText = sql;
return cmd.ExecuteScalar();

}

}



?

?

轉(zhuǎn)載于:https://www.cnblogs.com/xiaohaoblog/archive/2012/03/07/2384476.html

總結(jié)

以上是生活随笔為你收集整理的C#备份数据和还原数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。