CEL_Payroll/Payroll.BO/Query Analyzer/UtilsService.cs

167 lines
4.8 KiB
C#
Raw Permalink Normal View History

2024-09-17 14:30:13 +06:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace Payroll.BO
{
public class UtilsService
{
public UtilsService() { }
public void BackupDatabase(string filepath)
{
ETransactionContext tc = new ETransactionContext();
try
{
tc.Begin();
for (int i = 0; i <= 3; i++)
{
try
{
UtilsDA.BackupDatabase(tc, tc.Connection.DataBase, filepath);
break;
}
catch (Exception ex)
{
if (i == 3) throw new Exception(ex.Message);
}
}
tc.End();
}
catch(Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new Exception(e.Message);
#endregion
}
}
public ArrayList GetDatabases()
{
ArrayList dbList=new ArrayList();
ETransactionContext tc = new ETransactionContext();
try
{
tc.Begin();
IDataReader oReader = UtilsDA.GetDatabases(tc);
NullHandler oreader = new NullHandler(oReader);
while (oReader.Read())
{
dbList.Add(oreader.GetString(0));
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new Exception(e.Message);
#endregion
}
return dbList;
}
public void RestorDatabase(string dbName, string filepath)
{
ETransactionContext tc = new ETransactionContext();
try
{
tc.Begin();
for (int i = 0; i <= 3; i++)
{
try
{
UtilsDA.RestorDatabase(tc, dbName, filepath);
break;
}
catch (Exception ex)
{
if (i == 3) throw new Exception(ex.Message);
}
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new Exception(e.Message);
#endregion
}
}
public void RestorDatabase(string sDBName, string sBackupFilePath, string sToDataFilePath, string sToLogFilePath)
{
ETransactionContext tc = new ETransactionContext();
try
{
tc.Begin();
if (UtilsDA.IsExist(tc, sDBName)) throw new Exception(sDBName + " already exist.");
for (int i = 0; i <= 3; i++)
{
try
{
UtilsDA.RestorDatabase(tc, sDBName, sBackupFilePath, sToDataFilePath, sToLogFilePath);
break;
}
catch (Exception ex)
{
if (i == 3) throw new Exception(ex.Message);
}
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new Exception(e.Message);
#endregion
}
}
public void AttachSingleDB(string sFilePath, string sDBName)
{
ETransactionContext tc = new ETransactionContext();
try
{
tc.Begin();
if (UtilsDA.IsExist(tc,sDBName))
{
UtilsDA.RunCMD(tc,SQL.MakeSQL("EXEC sp_detach_db @dbname = %s",sDBName));
UtilsDA.RunCMD(tc, SQL.MakeSQL("EXEC sp_attach_single_file_db @dbname = %s, "
+ " @physname = %s",sDBName,sFilePath));
}
else
{
UtilsDA.RunCMD(tc, SQL.MakeSQL("exec sp_attach_single_file_db %s, %s", sDBName, sFilePath));
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new Exception(e.Message);
#endregion
}
}
}
}