C#备份数据和还原数据
生活随笔
收集整理的這篇文章主要介紹了
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();
}
}
{
//選擇要備份的路徑
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Azure Reporting
- 下一篇: 关于C#中实现两个应用程序消息通讯的问题