//还原的方法
public class DBstore
{
/// <summary>
/// 还原数据库文件
/// </summary>
/// <param name="dbFile">数据库备份文件(含路径)</param>
/// <param name = "DBName">恢复的数据库名称</param>
/// <returns></returns>
public bool DataRestoreConfigDB(string dbFile,string DBName)
{
//sql数据库名
//string dbName = "ZhangYuCeShi";
//创建连接对象
SqlConnection conn = new SqlConnection("Data Source=192.168.1.101;Initial Catalog=master;uid=anssy;pwd=Anssy3X;");
//还原指定的数据库文件
string sql = string.Format("use master ;declare @s varchar(8000);select @s=isnull(@s,'')+' kill '+rtrim(spID) from master..sysprocesses where dbid=db_id('{0}');select @s;exec(@s) ;RESTORE DATABASE {1} FROM DISK = N'{2}' with replace",DBName,DBName, dbFile);
SqlCommand sqlcmd = new SqlCommand(sql, conn);
sqlcmd.CommandType = CommandType.Text;
conn.Open();
try
{
sqlcmd.ExecuteNonQuery();
}
catch (Exception err)
{
string str = err.Message;
conn.Close();
return false;
}
conn.Close();//关闭数据库连接
return true;
}
}
//备份的方法
public ResultMsg BackUpDB()
{
string BackName = string.Format(@"d:/ZY{0}.bak", DateTime.Now.ToShortDateString().Replace("/","-"));
string SQL = string.Format("backup database {0} to disk = '{1}'",GetDBName(), BackName);
try
{
msg.ErrId = DapperDbHelper.Execute(SQL);
msg.ReturnString = BackName;
}
catch(Exception ee)
{
msg.ErrMsg =ee.Message;
}
return msg;
}
public string GetDBName()//获取数据库名
{
string filePath = Environment.CurrentDirectory + "\\Config\\DataBase.json";
JsonHelper jsonHelper = new JsonHelper();
jsonHelper.FilePath = filePath;
string text = jsonHelper.ReadJson();
text = Encryption.DecryptDES(text);
ConfigModel configModel = new ConfigModel();
configModel = jsonHelper.Deserialize<ConfigModel>(text);
return configModel.DataSource;
}