/// 對(duì)數(shù)據(jù)庫(kù)的備份和恢復(fù)操作,Sql語(yǔ)句實(shí)現(xiàn)
/// 實(shí)現(xiàn)備份或恢復(fù)的Sql語(yǔ)句
/// 該操作是否為備份操作,是為true否,為false
private void BakReductSql(string cmdText,bool isBak,string dbname)
{
string conString=DataBase.dbConnection.sqlNoDBNameConnect();
SqlConnection conn3 = new SqlConnection(conString);
SqlCommand cmdBakRst = new SqlCommand();
try
{
if(conn3.State==ConnectionState.Closed)
conn3.Open();
cmdBakRst.Connection = conn3;
cmdBakRst.CommandType = CommandType.Text;
if (!isBak) //如果是恢復(fù)操作
{
string setOffline = "Alter database "+dbname+" Set Offline With rollback immediate ";
string setOnline = " Alter database " + dbname + " Set Online With Rollback immediate";
cmdBakRst.CommandText = setOffline + cmdText + setOnline ;
}
else
{
cmdBakRst.CommandText = cmdText;
}
cmdBakRst.ExecuteNonQuery();
if (!isBak)
{
MessageBox.Show("數(shù)據(jù)成功恢復(fù)為所選文檔的狀態(tài)!", "系統(tǒng)消息");
}
else
{
MessageBox.Show("恭喜,你已經(jīng)成功備份當(dāng)前數(shù)據(jù)!", "系統(tǒng)消息");
}
}
catch (SqlException sexc)
{
MessageBox.Show("失敗,可能是對(duì)數(shù)據(jù)庫(kù)操作失敗,原因:" + sexc, "數(shù)據(jù)庫(kù)錯(cuò)誤消息");
}
catch (Exception ex)
{
MessageBox.Show("對(duì)不起,操作失敗,可能原因:" + ex, "系統(tǒng)消息");
}
finally
{
cmdBakRst.Dispose();
conn3.Close();
}
}
更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄