码迷,mamicode.com
首页 > 数据库 > 详细

c# 备份数据库恢复数据库

时间:2016-01-21 19:23:32      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
/// 对数据库的备份和恢复操作,Sql语句实现
/// </summary>
/// <param name="cmdText">实现备份或恢复的Sql语句</param>
/// <param name="isBak">该操作是否为备份操作,是为true否,为false</param>
private bool BakReductSql(string cmdText, bool isBak)
{
SqlCommand cmdBakRst = new SqlCommand();
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=master;uid=sa;pwd=zqnb_123;");
try
{
conn.Open();
cmdBakRst.Connection = conn;
cmdBakRst.CommandType = CommandType.Text;
if (!isBak) //如果是恢复操作
{
string set = "drop database [NbV3.1Db] ";
cmdBakRst.CommandText =set+ cmdText ;
}
else
{
cmdBakRst.CommandText = cmdText;
}
cmdBakRst.ExecuteNonQuery();
return true;
}
catch (SqlException sexc)
{
return false;
}
catch (Exception ex)
{
return false;
}
finally
{
cmdBakRst.Dispose();
conn.Close();
conn.Dispose();
}
}

private void button1_Click(object sender, EventArgs e)
{
string saveAway = @"d:\\1.bak";
string cmdText = @"backup database [NbV3.1Db] to disk=‘" + saveAway + "‘";
BakReductSql(cmdText, true);
}

private void button2_Click(object sender, EventArgs e)
{
string openAway = @"d:\\1.bak";
string cmdText = @"restore database [NbV3.1Db] from disk=‘" + openAway + "‘";
BakReductSql(cmdText, false);
}

c# 备份数据库恢复数据库

标签:

原文地址:http://www.cnblogs.com/zhtbk/p/5148716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!