/[AnywhereTS-MSSQL]/trunk/AnywhereTS.DBSupport/DBConnector.cs
ViewVC logotype

Diff of /trunk/AnywhereTS.DBSupport/DBConnector.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/AnywhereTS.DBSupport/DBConnector.cs	2012/07/14 09:08:55	119
+++ trunk/AnywhereTS.DBSupport/DBConnector.cs	2012/07/15 03:09:38	132
@@ -5,6 +5,8 @@
 using System.Data.Common;
 using System.Data;
 using System.Text.RegularExpressions;
+using System.IO;
+
 
 namespace AnywhereTS.DBSupport
 {
@@ -15,7 +17,7 @@
         where DBDataAdapter : DbDataAdapter, new()
     {
         bool ConnectionIsOpen { get; }
-        void CreateConnection(out Exception ErrorInfo);
+        DBConnection CreateConnection(out Exception ErrorInfo);
         void OpenConnection(out Exception ErrorInfo);
         void CloseConnection(out Exception ErrorInfo);
 
@@ -23,7 +25,7 @@
         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);
+        //DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo);
         bool RunScript(string strFile, out Exception ErrorInfo);
     }
     public abstract class DBConnector<DBParameter, DBCommand, DBConnection, DBDataAdapter> :
@@ -63,46 +65,54 @@
         public virtual bool ConnectionIsOpen { get; protected set; }
         #endregion
         #region public virtual DBCommand CreateCommandInstance(string command, List<DBParameter> Params, out Exception ErrorInfo)
-        public virtual 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;
-            }
-        }
+        //public virtual 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)
+        //    {
+        //        SqlException e = (ex.GetBaseException() as SqlException);
+        //        using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", e.Number.ToString(), e.Message, System.Environment.NewLine, e.ToString())))
+        //        {
+        //            Logging.DatabaseLog.Error(string.Format("Failed to create command instance using command: {0}", command));
+        //        }
+        //        ErrorInfo = ex; throw ErrorInfo;
+        //    }
+        //    catch (Exception ex)
+        //    {
+        //        Exception e = ex.GetBaseException();
+        //        using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", e.GetType().Name, e.Message, System.Environment.NewLine, e.ToString())))
+        //        {
+        //            Logging.DatabaseLog.Error(string.Format("Failed to create command instance using command: {0}", command));
+        //        }
+        //        ErrorInfo = ex; throw ErrorInfo;
+        //    }
+        //}
         #endregion
-        #region  public virtual void CreateConnection(out Exception ErrorInfo)
-        public virtual void CreateConnection(out Exception ErrorInfo)
+        #region  public virtual DBConnection CreateConnection(out Exception ErrorInfo)
+        public virtual DBConnection CreateConnection(out Exception ErrorInfo)
         {
             ErrorInfo = null;
+            string connetionString = string.Empty;
             try
-            {
-                string connetionString = null;
+            {                
                 connetionString = GetConnectionString();
-                connection = new DBConnection();
-                connection.ConnectionString = connetionString;
+                using (log4net.NDC.Push(string.Format("connetionString={0}", connetionString)))
+                {
+                    Logging.DatabaseLog.Debug("Creating Connection");
+                    connection = new DBConnection();
+                    connection.ConnectionString = connetionString;
+                    Logging.DatabaseLog.Debug("Created Connection");
+                }
+                return this.connection;
             }
             catch (SqlException ex)
             {
@@ -126,15 +136,27 @@
         public virtual void OpenConnection(out Exception ErrorInfo)
         {
             ErrorInfo = null;
+            string connetionString = string.Empty;
             try
             {
                 //this.CloseConnection(out ErrorInfo);
-                connection.Open();
-                this.ConnectionIsOpen = true;
+                connetionString = GetConnectionString();
+                using (log4net.NDC.Push(string.Format("connectionString={0}", connetionString)))
+                {
+                    Logging.DatabaseLog.Debug("Opening Connection");
+                    connection.Open();
+                    this.ConnectionIsOpen = true;
+                    Logging.DatabaseLog.Debug("Opened Connection");
+                }
             }
             //catch (SqlException ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex;  throw ErrorInfo;}
             catch (SqlException ex)
             {
+                if (ex.Message.ToLower().Contains(string.Format("Cannot open database").ToLower()))
+                {
+                    ErrorInfo = null;
+                    return;
+                }
                 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));
@@ -143,6 +165,11 @@
             }
             catch (Exception ex)
             {
+                if (ex.Message.ToLower().Contains(string.Format("Cannot open database").ToLower()))
+                {
+                    ErrorInfo = null;
+                    return;
+                }
                 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));
@@ -287,12 +314,8 @@
         }
         #endregion
         #region public string[] ParseScriptToCommands(string strScript)
-        public string[] ParseScriptToCommands(string strScript)
-        {
-            string[] commands;
-            commands = strScript.Split(new string[] { string.Format("GO{0}", System.Environment.NewLine) }, StringSplitOptions.RemoveEmptyEntries);
-            return commands;
-        }
+     
+        protected abstract bool ClientRunScript(string strFile, out Exception ErrorInfo);
         #endregion
         #region public virtual bool RunScript(string strFile, out Exception ErrorInfo)
         public bool RunScript(string strFile, out Exception ErrorInfo)
@@ -300,12 +323,12 @@
             ErrorInfo = null;
             try
             {
-                string[] strCommands;
-                strCommands = ParseScriptToCommands(strFile);
                 if (this.ConnectionIsOpen)
                 {
-                    if (!ClientRunScript(strCommands, out ErrorInfo))
+                    if (!ClientRunScript(strFile, out ErrorInfo))
                     {
+                        if(ErrorInfo != null)
+                            throw ErrorInfo;
                         return false;
                     }
                 }
@@ -317,23 +340,24 @@
             }
             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())))
+                SqlException e = (ex.GetBaseException() as SqlException);
+                using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", e.Number.ToString(), e.Message, System.Environment.NewLine, e.ToString())))
                 {
                     Logging.DatabaseLog.Error(string.Format("Failed to run script: {0}{1}", System.Environment.NewLine, strFile));
                 }
-                ErrorInfo = ex;
+                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())))
+                Exception e = ex.GetBaseException();
+                using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", e.GetType().Name, e.Message, System.Environment.NewLine, e.ToString())))
                 {
                     Logging.DatabaseLog.Error(string.Format("Failed to run script: {0}{1}", System.Environment.NewLine, strFile));
                 }
-                ErrorInfo = ex;
+                ErrorInfo = ex; throw ErrorInfo;
             }
             return false;
         }
-        public abstract bool ClientRunScript(string[] strCommands, out Exception ErrorInfo);
         #endregion
         #endregion
 
@@ -343,7 +367,8 @@
             try
             {
                 Exception ErrorInfo;
-                CloseConnection(out ErrorInfo);
+                if (this.ConnectionIsOpen)
+                    CloseConnection(out ErrorInfo);
             }
             catch
             {

 

  ViewVC Help
Powered by ViewVC 1.1.22