C# winfrom数据库备份与恢复
通过C# winfrom来对SQL数据库进行数据库的备份和还原
图1
图2
//设置保存的路径
private void btnBaoCun_Click(object sender, EventArgs e)
{
FolderBrowserDialog myFolderBrowserDialog = new FolderBrowserDialog();
//设置根目录在桌面;
myFolderBrowserDialog.RootFolder = System.Environment.SpecialFolder.Desktop;
//设置当前选择的路径
myFolderBrowserDialog.SelectedPath = "C:";
//允许在对话框中包括一个新建目录的按钮
myFolderBrowserDialog.ShowNewFolderButton = true;
//设置对话框的说明信息
myFolderBrowserDialog.Description = "请选择输出目录";
if (myFolderBrowserDialog.ShowDialog() == DialogResult.OK)
{//确认是否保存
string strLuJing = myFolderBrowserDialog.SelectedPath;//获取路径
txtBaoCunBeiFen.Text = strLuJing;//赋值给文本显示
}
}
/// <summary>
/// 创建连接字符串
/// </summary>
/// <param name="straddress">连接地址</param>
/// <param name="SQLname">备份的数据库名称</param>
/// <returns></returns>
private static SqlConnection GetConn(string straddress,string SQLname)
{//创建连接字符串与SQL连接,也可以直接调用DAL中的连接
SqlConnection conn = new SqlConnection(@"Data Source=" + straddress + ";Initial Catalog=" + SQLname + ";User ID=sa;Password=123");
return conn;
}
private void btnBaoCunBeiFen_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否备份数据", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
if (txtBaoCunBeiFen.Text.ToString() != "")
{
//设置连接字符串
SqlConnection conn = GetConn("10.20.0.25:14334","zbwx");
//实例化SQL可执行的存储过程
SqlCommand cmdBK = new SqlCommand();
//SQL文本
cmdBK.CommandType = CommandType.Text;
cmdBK.Connection = conn;
// DateTime dtm = new DateTime();
string strRiQi = DateTime.Now.Year.ToString() + (DateTime.Now.Month.ToString().Length < 2 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString()) + (DateTime.Now.Day.ToString().Length < 2 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString()) + (DateTime.Now.Hour.ToString().Length < 2 ? "0" + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString()) + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
cmdBK.CommandText = @"backup database zbwx to disk='" + txtBaoCunBeiFen.Text + "\\" + strRiQi + "" + ".bak'";
try
{
//进入SQL
conn.Open();
//返回影响行数
cmdBK.ExecuteNonQuery();
MessageBox.Show("备份成功!");
this.Dispose();//释放资源
this.Close();//关闭
}
catch (Exception)
{
MessageBox.Show("备份失败");
}
finally
{
conn.Close();//关闭与SQL的连接
}
}
else
{
MessageBox.Show("请选择保存路径!");
}
}
}
private void btnHuanYuan_Click(object sender, EventArgs e)
{
//文件控件
OpenFileDialog filename = new OpenFileDialog();
//获取路径
filename.InitialDirectory = Application.StartupPath;
//设置可打开的文件格式
filename.Filter = "All files (*.*)|*.bak";
filename.FilterIndex = 2;
//是否还原当前路径
filename.RestoreDirectory = true;
if (filename.ShowDialog() == DialogResult.OK)
{
//处理路径
string path = filename.FileName.ToString();
string Name = path.Substring(path.LastIndexOf("\\") + 1);
txtBaoCunWenJian.Text = path;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否还原数据", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
if (txtBaoCunWenJian.Text.ToString() != "")
{
string databasefile=txtBaoCunWenJian.Text;
MessageBox.Show(databasefile);
if (RestoreDataBase("zbwx", databasefile))
{
MessageBox.Show("还原成功!");
}
else {
MessageBox.Show("还原失败!");
}
this.Dispose();
this.Close();
}
else
{
MessageBox.Show("请选择文件路径!");
}
}
}
private void frmShuJuBeiFenYuHuiFu_Load(object sender, EventArgs e)
{
string strMoRen = System.Environment.CurrentDirectory;
txtBaoCunBeiFen.Text = strMoRen;
}
SqlConnection constring = new SqlConnection("Data Source=(local);Initial Catalog=master;User ID=sa;Password=123");
/// <summary>
/// 还原数据库
/// </summary>
/// <param name="databasename">需要还原数据库的名称</param>
/// <param name="databasefile">文件路径</param>
/// <returns></returns>
public bool RestoreDataBase(string databasename,string databasefile)
{
// SqlConnection constring = new SqlConnection("Data Source=(local);Initial Catalog=master;User ID=sa;Password=123");
string sql = " RESTORE DATABASE " + databasename + " from DISK ='" + databasefile + "'" + " WITH REPLACE";//数据库名称和路径 WITH REPLACE是去除日志文件
SqlCommand sqlcmd = new SqlCommand(sql, constring);
sqlcmd.CommandType = CommandType.Text;
try {
//开始
constring.Open();
sqlcmd.ExecuteNonQuery();
}catch(Exception ex)
{
string str = ex.Message;
constring.Close();
return false;
}
constring.Close();//结束
return true;
}
该资料仅供学习,禁止商业用途