نوشته شده توسط
csharpprogramer88
دوستان تو را به خد ا بررسي كنيد
backup:
#region CreateBackup
public static bool CreateBackup(string filename)
{
bool sign = false;
string file = Path.GetFileName(filename);
string backupDir = "";
RegistryKey registry;
try
{
registry = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenS ubKey("Microsoft").OpenSubKey("Microsoft SQL Server").OpenSubKey("MSSQL.1").OpenSubKey("MSSQLSe rver");
backupDir = registry.GetValue("BackupDirectory").ToString();
}
catch (Exception ex)
{
Error = ex.Message;
//throw new Exception(" .\n .", ex);
}
string dbName = GetDatabaseName();
//server = GetDatabaseServer();
try
{
IDbConnection connection = newSqlConnection(string.Format("Server={1};initial catalog={0};Integrated Security=SSPI", dbName, server));
connection.Open();
IDbCommand command = connection.CreateCommand();
command.CommandText = "BACKUP DATABASE " + dbName + " TO DISK=N'" + file + "' WITH FORMAT";
command.ExecuteNonQuery();
File.Copy(backupDir + "\\" + file, filename, true);
File.Delete(backupDir + "\\" + file);
connection.Close();
sign = true;
}
catch (Exception ex)
{
Error += ex.Message;
sign = false;
}
//finally
//{
// try
// { connection.Close(); }
// catch (Exception ex) { }
//}
return sign;
}
#endregion
restore
#region RestoreBackup
public static bool RestoreBackup(string filename, string Database_Name)
{
bool sign = false;
// server = ".\\SQLEXPRESS";
string file = Path.GetFileName(filename);
string backupDir = "";
RegistryKey registry;
try
{
registry = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenS ubKey("Microsoft").OpenSubKey("Microsoft SQL Server").OpenSubKey("MSSQL.1").OpenSubKey("MSSQLSe rver");
backupDir = registry.GetValue("BackupDirectory").ToString();
}
catch (Exception ex)
{
Error = ex.Message;
//throw new Exception(" .\n .", ex);
}
string dbName = GetDatabaseName();
IDbConnection connection = newSqlConnection(string.Format("Server={1};initial catalog={0};Integrated Security=SSPI", dbName, server));
connection.Open();
IDbCommand command = connection.CreateCommand();
string defaultDB = connection.Database;
string tmpBackupFile = backupDir + "\\" + file;
System.Data.IDbTransaction tran;
try
{
//System.Data.SqlClient.SqlTransaction transaction;
//tran = connection.BeginTransaction(IsolationLevel.ReadCom mitted);
//command.Transaction = tran;
SqlCommand Cmd_Single = newSqlCommand();
//Cmd_Single.Connection = con;
string Cmd_Singletext = "ALTER DATABASE " + dbName + " SET SINGLE_USER with ROLLBACK IMMEDIATE";
command.CommandText = Cmd_Singletext;
command.ExecuteNonQuery();
File.Copy(filename, tmpBackupFile, true);
connection.ChangeDatabase("master");
command.CommandText = "RESTORE DATABASE " + defaultDB + " FROM DISK=N'" + tmpBackupFile + "' WITH REPLACE";
command.ExecuteNonQuery();
connection.ChangeDatabase(defaultDB);
File.Delete(tmpBackupFile);
Cmd_Singletext = "ALTER DATABASE " + dbName + " SET MULTI_USER";
command.CommandText = Cmd_Singletext;
command.ExecuteNonQuery();
sign = true;
// tran.Commit();
}
catch (Exception ex)
{
Error = ex.Message;
sign = false;
// Attempt to roll back the transaction.
try
{
// tran.Rollback();
}
catch (Exception ex2)
{
Error += ex2.Message;
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
connection.Close();
return sign;
}
#endregion