Author: suresh
Date: 2005-03-11 02:25:22 -0500 (Fri, 11 Mar 2005)
New Revision: 41678

Modified:
   trunk/mcs/class/System.Data/System.Data.Odbc/ChangeLog
   trunk/mcs/class/System.Data/System.Data.Odbc/OdbcConnection.cs
Log:
2005-03-11  Sureshkumar T  <[EMAIL PROTECTED]>

        * OdbcConnection.cs : notify state change through event. clean up
        handles in case any exception while opening a connection.



Modified: trunk/mcs/class/System.Data/System.Data.Odbc/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data/System.Data.Odbc/ChangeLog      2005-03-11 
07:08:20 UTC (rev 41677)
+++ trunk/mcs/class/System.Data/System.Data.Odbc/ChangeLog      2005-03-11 
07:25:22 UTC (rev 41678)
@@ -1,3 +1,8 @@
+2005-03-11  Sureshkumar T  <[EMAIL PROTECTED]>
+
+       * OdbcConnection.cs : notify state change through event. clean up
+       handles in case any exception while opening a connection.
+
 2005-03-10  Sureshkumar T  <[EMAIL PROTECTED]>
 
        Migrated core classes to derive from ProviderBase instead of

Modified: trunk/mcs/class/System.Data/System.Data.Odbc/OdbcConnection.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data.Odbc/OdbcConnection.cs      
2005-03-11 07:08:20 UTC (rev 41677)
+++ trunk/mcs/class/System.Data/System.Data.Odbc/OdbcConnection.cs      
2005-03-11 07:25:22 UTC (rev 41678)
@@ -253,22 +253,11 @@
                                if ( (ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
                                        throw new OdbcException (new OdbcError 
("SQLDisconnect", OdbcHandleType.Dbc,hdbc));
 
-                               // free handles
-                               if (hdbc != IntPtr.Zero) {
-                                       ret = libodbc.SQLFreeHandle ( (ushort) 
OdbcHandleType.Dbc, hdbc);       
-                                       if ( (ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
-                                               throw new OdbcException (new 
OdbcError ("SQLFreeHandle", OdbcHandleType.Dbc,hdbc));
-                               }
-                               hdbc = IntPtr.Zero;
+                               FreeHandles ();
 
-                               if (henv != IntPtr.Zero) {
-                                       ret = libodbc.SQLFreeHandle ( (ushort) 
OdbcHandleType.Env, henv);       
-                                       if ( (ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
-                                               throw new OdbcException (new 
OdbcError ("SQLFreeHandle", OdbcHandleType.Env,henv));
-                               }
-                               henv = IntPtr.Zero;
+                               transaction = null;
 
-                               transaction = null;
+                                RaiseEventStateChange (ConnectionState.Open, 
ConnectionState.Closed);
                        }
                }
 
@@ -332,30 +321,31 @@
 
                        OdbcReturn ret = OdbcReturn.Error;
                
-                       // allocate Environment handle  
-                       ret = libodbc.SQLAllocHandle (OdbcHandleType.Env, 
IntPtr.Zero, ref henv);
-                       if ( (ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
-                               throw new OdbcException (new OdbcError 
("SQLAllocHandle"));
+                        try {
+                                // allocate Environment handle 
+                                ret = libodbc.SQLAllocHandle 
(OdbcHandleType.Env, IntPtr.Zero, ref henv);
+                                if ( (ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
+                                        throw new OdbcException (new OdbcError 
("SQLAllocHandle"));
                
-                       ret=libodbc.SQLSetEnvAttr (henv, OdbcEnv.OdbcVersion, 
(IntPtr) libodbc.SQL_OV_ODBC3 , 0); 
-                       if ((ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
-                               throw new OdbcException (new OdbcError 
("SQLSetEnvAttr", OdbcHandleType.Env,henv));
+                                ret=libodbc.SQLSetEnvAttr (henv, 
OdbcEnv.OdbcVersion, (IntPtr) libodbc.SQL_OV_ODBC3 , 0); 
+                                if ((ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
+                                        throw new OdbcException (new OdbcError 
("SQLSetEnvAttr", OdbcHandleType.Env,henv));
                
-                       // allocate connection handle
-                       ret=libodbc.SQLAllocHandle (OdbcHandleType.Dbc, henv, 
ref hdbc);
-                       if ( (ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
-                               throw new OdbcException (new OdbcError 
("SQLAllocHandle",OdbcHandleType.Env,henv));
+                                // allocate connection handle
+                                ret=libodbc.SQLAllocHandle 
(OdbcHandleType.Dbc, henv, ref hdbc);
+                                if ( (ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
+                                        throw new OdbcException (new OdbcError 
("SQLAllocHandle",OdbcHandleType.Env,henv));
                        
-                       // DSN connection
-                       if (ConnectionString.ToLower().IndexOf("dsn=")>=0)
-                       {
-                               string _uid="", _pwd="", _dsn="";
-                               string[] items=ConnectionString.Split(new 
char[1]{';'});
-                               foreach (string item in items)
-                               {
-                                       string[] parts=item.Split(new char[1] 
{'='});
-                                       switch (parts[0].Trim().ToLower())
-                                       {
+                                // DSN connection
+                                if 
(ConnectionString.ToLower().IndexOf("dsn=")>=0)
+                                {
+                                        string _uid="", _pwd="", _dsn="";
+                                        string[] 
items=ConnectionString.Split(new char[1]{';'});
+                                        foreach (string item in items)
+                                        {
+                                                string[] parts=item.Split(new 
char[1] {'='});
+                                                switch 
(parts[0].Trim().ToLower())
+                                                {
                                                case "dsn":
                                                        _dsn=parts[1].Trim();
                                                        break;
@@ -365,23 +355,28 @@
                                                case "pwd":
                                                        _pwd=parts[1].Trim();
                                                        break;
-                                       }
-                               }
-                               ret=libodbc.SQLConnect(hdbc, _dsn, -3, _uid, 
-3, _pwd, -3);
-                               if ((ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
-                                       throw new OdbcException(new 
OdbcError("SQLConnect",OdbcHandleType.Dbc,hdbc));
-                       }
-                       else 
-                       {
-                               // DSN-less Connection
-                               string OutConnectionString=new String(' ',1024);
-                               short OutLen=0;
-                               ret=libodbc.SQLDriverConnect(hdbc, IntPtr.Zero, 
ConnectionString, -3, 
-                                       OutConnectionString, (short) 
OutConnectionString.Length, ref OutLen, 0);
-                               if ((ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
-                                       throw new OdbcException(new 
OdbcError("SQLDriverConnect",OdbcHandleType.Dbc,hdbc));
-                       }
+                                                }
+                                        }
+                                        ret=libodbc.SQLConnect(hdbc, _dsn, -3, 
_uid, -3, _pwd, -3);
+                                        if ((ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
+                                                throw new OdbcException(new 
OdbcError("SQLConnect",OdbcHandleType.Dbc,hdbc));
+                                }
+                                else 
+                                {
+                                        // DSN-less Connection
+                                        string OutConnectionString=new 
String(' ',1024);
+                                        short OutLen=0;
+                                        ret=libodbc.SQLDriverConnect(hdbc, 
IntPtr.Zero, ConnectionString, -3, 
+                                                                     
OutConnectionString, (short) OutConnectionString.Length, ref OutLen, 0);
+                                        if ((ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
+                                                throw new OdbcException(new 
OdbcError("SQLDriverConnect",OdbcHandleType.Dbc,hdbc));
+                                }
 
+                                RaiseEventStateChange (ConnectionState.Closed, 
ConnectionState.Open);
+                        } catch (Exception) {
+                                // free handles if any.
+                                FreeHandles ();
+                        }
                }
 
                [MonoTODO]
@@ -390,6 +385,26 @@
                        throw new NotImplementedException ();
                }
 
+                private void FreeHandles ()
+                {
+                        OdbcReturn ret = OdbcReturn.Error;
+                        if (hdbc != IntPtr.Zero) {
+                                ret = libodbc.SQLFreeHandle ( (ushort) 
OdbcHandleType.Dbc, hdbc);      
+                                if ( (ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
+                                        throw new OdbcException (new OdbcError 
("SQLFreeHandle", OdbcHandleType.Dbc,hdbc));
+                        }
+                        hdbc = IntPtr.Zero;
+
+                        if (henv != IntPtr.Zero) {
+                                ret = libodbc.SQLFreeHandle ( (ushort) 
OdbcHandleType.Env, henv);      
+                                if ( (ret!=OdbcReturn.Success) && 
(ret!=OdbcReturn.SuccessWithInfo)) 
+                                        throw new OdbcException (new OdbcError 
("SQLFreeHandle", OdbcHandleType.Env,henv));
+                        }
+                        henv = IntPtr.Zero;
+                                
+                }
+                
+
                [MonoTODO]
                public
 #if NET_2_0
@@ -419,6 +434,13 @@
                 }
 
 
+                private void RaiseEventStateChange (ConnectionState from, 
ConnectionState to)
+                {
+                        if (StateChange != null)
+                                StateChange (this, new StateChangeEventArgs 
(from, to));
+                }
+                
+
                #endregion
 
                #region Events and Delegates

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to