Parent Directory
|
Revision Log
|
Patch
--- trunk/AnywhereTS.DBSupport/DBConnector.cs 2012/07/13 21:10:26 85 +++ trunk/AnywhereTS.DBSupport/DBConnector.cs 2012/07/13 22:37:40 87 @@ -4,6 +4,7 @@ using System.Data.SqlClient; using System.Data.Common; using System.Data; +using System.Text.RegularExpressions; namespace AnywhereTS.DBSupport { @@ -21,6 +22,9 @@ DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo); void ExecuteNonQuery(string command, List<DBParameter> Params, out Exception ErrorInfo); List<string> ExecuteColumnNamesReader(string command, List<DBParameter> Params, out Exception ErrorInfo); + + DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo); + public bool RunScript(string strFile, MsSqlConnector con, out Exception ErrorInfo); } public abstract class DBConnector<DBParameter, DBCommand, DBConnection, DBDataAdapter> : IDBConnector<DBParameter, DBCommand, DBConnection, DBDataAdapter> @@ -32,14 +36,46 @@ public DBConnector(string DBServerAddress, string DBServerInstance, string DBDatabase) { + this.DBServerAddress = DBServerAddress; + this.DBServerInstance = DBServerInstance; + this.DBDatabase = DBDatabase; } protected DBConnection connection; - protected static string DBServerAddress = ""; - protected static string DBServerInstance = ""; - protected static string DBDatabase = ""; + protected string DBServerAddress = ""; + protected string DBServerInstance = ""; + protected string DBDatabase = ""; private string SafeSqlLiteral(string inputSQL) { return inputSQL.Replace("'", "''"); } #region IDBConnector members + public DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo) + { + ErrorInfo = null; + try + { + command = this.SafeSqlLiteral(command); + DBCommand sqlComm = new DBCommand(); + sqlComm.CommandText = command; + sqlComm.Connection = connection; + foreach (DBParameter p in Params) { sqlComm.Parameters.Add(p); } + return sqlComm; + } + catch (SqlException ex) + { + using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to create command instance using command: {0}", command)); + } + ErrorInfo = ex; throw ErrorInfo; + } + catch (Exception ex) + { + using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to create command instance using command: {0}", command)); + } + ErrorInfo = ex; throw ErrorInfo; + } + } private bool _ConnectionIsOpen; public virtual bool ConnectionIsOpen { get { return _ConnectionIsOpen; } protected set { _ConnectionIsOpen = value; } } public virtual void CreateConnection(out Exception ErrorInfo) @@ -52,8 +88,22 @@ connection = new DBConnection(); connection.ConnectionString = connetionString; } - //catch (SqlException ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex; throw ErrorInfo;} - catch (Exception ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex; throw ErrorInfo; } + catch (SqlException ex) + { + using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to create connection to {0} Database", DBDatabase)); + } + ErrorInfo = ex; throw ErrorInfo; + } + catch (Exception ex) + { + using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to create connection to {0} Database", DBDatabase)); + } + ErrorInfo = ex; throw ErrorInfo; + } } public virtual void OpenConnection(out Exception ErrorInfo) { @@ -65,7 +115,22 @@ this.ConnectionIsOpen = true; } //catch (SqlException ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex; throw ErrorInfo;} - catch (Exception ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex; throw ErrorInfo; } + catch (SqlException ex) + { + using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to open connection to {0} Database", DBDatabase)); + } + ErrorInfo = ex; throw ErrorInfo; + } + catch (Exception ex) + { + using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to open connection to {0} Database", DBDatabase)); + } + ErrorInfo = ex; throw ErrorInfo; + } } public virtual void CloseConnection(out Exception ErrorInfo) { @@ -75,8 +140,22 @@ if (this.ConnectionIsOpen) connection.Close(); } - //catch (SqlException ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex; throw ErrorInfo;} - catch (Exception ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex; throw ErrorInfo; } + catch (SqlException ex) + { + using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to close connection to {0} Database", DBDatabase)); + } + ErrorInfo = ex; throw ErrorInfo; + } + catch (Exception ex) + { + using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to close connection to {0} Database", DBDatabase)); + } + ErrorInfo = ex; throw ErrorInfo; + } } public virtual DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo) @@ -93,8 +172,22 @@ DbDataReader r = sqlComm.ExecuteReader(); return r; } - catch (Exception ex) { ErrorInfo = new Exception("Cannot execute query.", ex); } - return null; + catch (SqlException ex) + { + using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to execute query: {0}", command)); + } + ErrorInfo = ex; throw ErrorInfo; + } + catch (Exception ex) + { + using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to execute querey: {0}", command)); + } + ErrorInfo = ex; throw ErrorInfo; + } } public virtual void ExecuteNonQuery(string command, List<DBParameter> Params, out Exception ErrorInfo) { @@ -109,7 +202,22 @@ foreach (DBParameter p in Params) { sqlComm.Parameters.Add(p); } sqlComm.ExecuteNonQuery(); } - catch (Exception ex) { ErrorInfo = new Exception("Cannot execute non-query.", ex); } + catch (SqlException ex) + { + using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to execute non querey: {0}", command)); + } + ErrorInfo = ex; throw ErrorInfo; + } + catch (Exception ex) + { + using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to execute non querey: {0}", command)); + } + ErrorInfo = ex; throw ErrorInfo; + } } public virtual List<string> ExecuteColumnNamesReader(string command, List<DBParameter> Params, out Exception ErrorInfo) { @@ -135,10 +243,73 @@ } return ColumnNames; } - catch (Exception ex) { ErrorInfo = new Exception("Cannot get column names from reader.", ex); } - return new List<string>(); + catch (SqlException ex) + { + using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to get colum names from reader: {0}", command)); + } + ErrorInfo = ex; throw ErrorInfo; + } + catch (Exception ex) + { + using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to get colum names from reader: {0}", command)); + } + ErrorInfo = ex; throw ErrorInfo; + } + } + public string[] ParseScriptToCommands(string strScript) + { + string[] commands; + commands = Regex.Split(strScript, "GO\r\n", RegexOptions.IgnoreCase); + return commands; + } + public virtual bool RunScript(string strFile, out Exception ErrorInfo) + { + ErrorInfo = null; + try + { + string[] strCommands; + strCommands = ParseScriptToCommands(strFile); + if (this.ConnectionIsOpen) + { + foreach (string strCmd in strCommands) + { + if (strCmd.Length > 0) + { + // Substitute database directory with the decided one. + DBCommand command = this.CreateCommandInstance(strCmd, new List<DBParameter>(), out ErrorInfo); + command.ExecuteNonQuery(); + } + } + return true; + } + else + { + Logging.ATSAdminLog.Fatal(string.Format("Failed to run script: [database connection is not open] {0}{1}", System.Environment.NewLine, strFile)); + return false; + } + } + catch (SqlException ex) + { + using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to run script: {0}{1}", System.Environment.NewLine, strFile)); + } + ErrorInfo = ex; + } + catch (Exception ex) + { + using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString()))) + { + Logging.DatabaseLog.Error(string.Format("Failed to run script: {0}{1}", System.Environment.NewLine, strFile)); + } + ErrorInfo = ex; + } + return false; } - #endregion #region IDisposable Members
ViewVC Help | |
Powered by ViewVC 1.1.22 |