/[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/13 22:56:50	89
+++ trunk/AnywhereTS.DBSupport/DBConnector.cs	2012/07/14 11:44:52	125
@@ -5,6 +5,8 @@
 using System.Data.Common;
 using System.Data;
 using System.Text.RegularExpressions;
+using System.IO;
+
 
 namespace AnywhereTS.DBSupport
 {
@@ -34,16 +36,20 @@
         where DBDataAdapter : DbDataAdapter, new()
     {
 
-        public DBConnector(string Address, string Instance, string Database)
+        public DBConnector(string Server, string Instance, string Database)
         {
-            DBServerAddress = Address;
+            DBServerAddress = Server;
             DBServerInstance = Instance;
             DBDatabase = Database;
+            using (log4net.NDC.Push(string.Format("[Server={0}] [Instance={1}] [Database={2}]", Server, Instance, Database)))
+            {
+                Logging.DatabaseLog.Debug("Creating DBConnector instance");
+            }
         }
 
         public static string GetConnectionString()
         {
-            return string.Format(@"Data Source={0}\{1};Initial Catalog={2};Integrated Security=SSPI", DBServerAddress, DBServerInstance, DBDatabase);
+            return string.Format(@"Data Source={0}\{1};Database={2};Integrated Security=SSPI", DBServerAddress, DBServerInstance, DBDatabase);
         }
         protected DBConnection connection;
         #region DBServerAddress, DBServerInstance, DBDatabase
@@ -93,12 +99,17 @@
         public virtual void 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");
+                }
             }
             catch (SqlException ex)
             {
@@ -122,11 +133,18 @@
         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)
@@ -283,37 +301,27 @@
         }
         #endregion
         #region public string[] ParseScriptToCommands(string strScript)
-        public string[] ParseScriptToCommands(string strScript)
-        {
-            string[] commands;
-            commands = Regex.Split(strScript, "GO\r\n", RegexOptions.IgnoreCase);
-            return commands;
-        }
+     
+        protected abstract bool ClientRunScript(string strFile, out Exception ErrorInfo);
         #endregion
         #region public virtual bool RunScript(string strFile, out Exception ErrorInfo)
-        public virtual bool RunScript(string strFile, out Exception ErrorInfo)
+        public bool RunScript(string strFile, out Exception ErrorInfo)
         {
             ErrorInfo = null;
             try
             {
-                string[] strCommands;
-                strCommands = ParseScriptToCommands(strFile);
                 if (this.ConnectionIsOpen)
                 {
-                    foreach (string strCmd in strCommands)
+                    if (!ClientRunScript(strFile, out ErrorInfo))
                     {
-                        if (strCmd.Length > 0)
-                        {
-                            // Substitute database directory with the decided one.
-                            DBCommand command = this.CreateCommandInstance(strCmd, new List<DBParameter>(), out ErrorInfo);
-                            command.ExecuteNonQuery();
-                        }
+                        if(ErrorInfo != null)
+                            throw ErrorInfo;
+                        return false;
                     }
-                    return true;
                 }
                 else
                 {
-                    Logging.ATSAdminLog.Fatal(string.Format("Failed to run script: [database connection is not open] {0}{1}", System.Environment.NewLine, strFile));
+                    Logging.DatabaseLog.Fatal(string.Format("Failed to run script: [database connection is not open] {0}{1}", System.Environment.NewLine, strFile));
                     return false;
                 }
             }

 

  ViewVC Help
Powered by ViewVC 1.1.22