private void InternalOpen(string connnectionString, DB2Connection connection) { try { DB2Constants.RetCode sqlRet = (DB2Constants.RetCode)DB2CLIWrapper.SQLAllocHandle(DB2Constants.SQL_HANDLE_DBC, DB2Environment.Instance.PenvHandle, out dbHandle); DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, DB2Environment.Instance.PenvHandle, "Unable to allocate database handle in DB2Connection.", connection); StringBuilder outConnectStr = new StringBuilder(DB2Constants.SQL_MAX_OPTION_STRING_LENGTH); short numOutCharsReturned; sqlRet = (DB2Constants.RetCode)DB2CLIWrapper.SQLDriverConnect(dbHandle, IntPtr.Zero, connnectionString, DB2Constants.SQL_NTS, outConnectStr, DB2Constants.SQL_MAX_OPTION_STRING_LENGTH, out numOutCharsReturned, DB2Constants.SQL_DRIVER_NOPROMPT); DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, dbHandle, "Unable to connect to the database.", connection); databaseProductName = SQLGetInfo(dbHandle, DB2Constants.SQL_DBMS_NAME); databaseVersion = SQLGetInfo(dbHandle, DB2Constants.SQL_DBMS_VER); /* Set the attribute SQL_ATTR_XML_DECLARATION to skip the XML declaration from XML Data */ sqlRet = (DB2Constants.RetCode)DB2CLIWrapper.SQLSetConnectAttr(dbHandle, DB2Constants.SQL_ATTR_XML_DECLARATION, new IntPtr(DB2Constants.SQL_XML_DECLARATION_NONE), DB2Constants.SQL_NTS); DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, dbHandle, "Unable to set SQL_ATTR_XML_DECLARATION", connection); connection.NativeOpenPerformed = true; if ((settings.Pool == null) || (settings.Pool.databaseProductName == null)) { settings.Pool.databaseProductName = databaseProductName; settings.Pool.databaseVersion = databaseVersion; } else if (settings.Pool != null) { if (settings.Pool != null) { databaseProductName = settings.Pool.databaseProductName; databaseVersion = settings.Pool.databaseVersion; } } } catch { if (dbHandle != IntPtr.Zero) { DB2CLIWrapper.SQLFreeHandle(DB2Constants.SQL_HANDLE_DBC, dbHandle); dbHandle = IntPtr.Zero; } throw; } }
public DB2OpenConnection(DB2ConnectionSettings settings, DB2Connection connection) { this.settings = settings; try { short sqlRet = DB2CLIWrapper.SQLAllocHandle(DB2Constants.SQL_HANDLE_DBC, DB2Environment.Instance.penvHandle, out dbHandle); DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, DB2Environment.Instance.penvHandle, "Unable to allocate database handle in DB2Connection.", connection); if (settings.Server.Length > 0) { StringBuilder outConnectStr = new StringBuilder(60); // TODO: ???? short numOutCharsReturned; sqlRet = DB2CLIWrapper.SQLDriverConnect(dbHandle, IntPtr.Zero, settings.ConnectionString, (short)settings.ConnectionString.Length, outConnectStr, (short)outConnectStr.Length, out numOutCharsReturned, DB2Constants.SQL_DRIVER_NOPROMPT /*SQL_DRIVER_COMPLETE*/); } else { sqlRet = DB2CLIWrapper.SQLConnect(dbHandle, settings.DatabaseAlias, (short)settings.DatabaseAlias.Length, settings.UserName, (short)settings.UserName.Length, settings.PassWord, (short)settings.PassWord.Length); DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, dbHandle, "Unable to connect to the database.", connection); } if ((settings.Pool == null) || (settings.Pool.databaseProductName == null)) { StringBuilder sb = new StringBuilder(256); short stringLength; sqlRet = DB2CLIWrapper.SQLGetInfo(dbHandle, /*SQL_DBMS_NAME*/ 17, sb, (short)(sb.Capacity / 2), out stringLength); new DB2ErrorCollection(DB2Constants.SQL_HANDLE_DBC, dbHandle).ToString(); if (sqlRet == 0) { databaseProductName = sb.ToString(0, Math.Min(sb.Capacity, stringLength / 2)); } sqlRet = DB2CLIWrapper.SQLGetInfo(dbHandle, /*SQL_DBMS_VER*/ 18, sb, (short)(sb.Capacity / 2), out stringLength); if (sqlRet == 0) { databaseVersion = sb.ToString(0, Math.Min(sb.Capacity, stringLength / 2)); try { string[] splitVersion = databaseVersion.Split('.'); majorVersion = int.Parse(splitVersion[0]); minorVersion = int.Parse(splitVersion[1]); } catch {} } if (settings.Pool != null) { settings.Pool.databaseProductName = databaseProductName; settings.Pool.databaseVersion = databaseVersion; settings.Pool.majorVersion = majorVersion; settings.Pool.minorVersion = minorVersion; } } else if (settings.Pool != null) { if (settings.Pool != null) { databaseProductName = settings.Pool.databaseProductName; databaseVersion = settings.Pool.databaseVersion; majorVersion = settings.Pool.majorVersion; minorVersion = settings.Pool.minorVersion; } } } catch { if (dbHandle != IntPtr.Zero) { DB2CLIWrapper.SQLFreeHandle(DB2Constants.SQL_HANDLE_DBC, dbHandle); dbHandle = IntPtr.Zero; } throw; } }