Пример #1
0
        internal static ODBCResult GetDataSources(ODBCHEnvironment enviromentHandle, ushort direction, out StringBuilder serverName, out StringBuilder description)
        {
            serverName  = new StringBuilder();
            description = new StringBuilder();
            short nameLength1Ptr = 0;
            short bufferLength1  = ODBCConstants.MAX_DSN_LENGTH + 1;
            short bufferLength2  = 0;
            short nameLength2Ptr = 0;
            var   res            = ODBCNativeMethods.SQLDataSources(enviromentHandle, direction, null, bufferLength1, out nameLength1Ptr, null, bufferLength2, out nameLength2Ptr);

            if ((res == ODBCResult.Success) || (res == ODBCResult.SuccessWithInfo))
            {
                if (nameLength1Ptr > 0)
                {
                    serverName.Capacity = nameLength1Ptr + 2;
                    bufferLength1       = (short)(nameLength1Ptr + 2);
                    if (nameLength2Ptr > 0)
                    {
                        description.Capacity = nameLength2Ptr + 2;
                        bufferLength2        = (short)(nameLength2Ptr + 2);
                    }
                    res = ODBCNativeMethods.SQLDataSources(enviromentHandle, direction, serverName, bufferLength1, out nameLength1Ptr, description, bufferLength2, out nameLength2Ptr);
                    if ((res != ODBCResult.Success) && (res != ODBCResult.SuccessWithInfo) && (res != ODBCResult.NoData))
                    {
                        var ex = GetException(enviromentHandle, "Unable to get ODBC data sources list ");
                        throw ex;
                    }
                }
            }
            return(res);
        }
Пример #2
0
        private static List <ODBCErrorInfo> GetErrorInfo(ODBCHBase handle)
        {
            var   eList         = new List <ODBCErrorInfo>();
            short start         = 1;
            var   messageBuffer = new StringBuilder(256);
            var   stateBuffer   = new StringBuilder(5);
            int   nativeError;
            short bufferLengthNeeded;
            var   resultString = new StringBuilder();

            while (true)
            {
                var result = ODBCNativeMethods.SQLGetDiagRecW(handle.HandleType, handle, start, stateBuffer, out nativeError, messageBuffer, (short)messageBuffer.Capacity, out bufferLengthNeeded);
                if (result == ODBCResult.SuccessWithInfo)
                {
                    bufferLengthNeeded = (short)((int)bufferLengthNeeded + 1);
                    if (messageBuffer.Capacity < bufferLengthNeeded)
                    {
                        messageBuffer.Capacity = bufferLengthNeeded;
                        result = ODBCNativeMethods.SQLGetDiagRecW(handle.HandleType, handle, start, stateBuffer, out nativeError, messageBuffer, (short)messageBuffer.Capacity, out bufferLengthNeeded);
                    }
                }
                if ((result != ODBCResult.Success) && (result != ODBCResult.SuccessWithInfo))
                {
                    break;
                }
                eList.Add(new ODBCErrorInfo(stateBuffer.ToString(), nativeError.ToString(), messageBuffer.ToString()));
                start++;
            }
            return(eList);
        }
Пример #3
0
        public static bool ExecDirect(ODBCHStatement statementHandle, string statementText, int textLength, out string info)
        {
            info = string.Empty;
            if (statementText == null)
            {
                throw new ArgumentNullException("Command string is empty");
            }
            if (statementText.Trim() == "")
            {
                throw new ArgumentException("Command string is empty");
            }

            var result = ODBCNativeMethods.SQLExecDirectW(statementHandle, statementText, textLength);

            if ((result != ODBCResult.Success) & (result != ODBCResult.SuccessWithInfo))
            {
                throw GetException(statementHandle, "Unnable to execute sql command " + statementText);
            }
            if (result == ODBCResult.SuccessWithInfo)
            {
                var list_info = GetErrorInfo(statementHandle);
                if (list_info.Count > 0)
                {
                    info = list_info[0].EMessage;
                }
            }

            return(true);
        }
Пример #4
0
        internal static bool GetConnectionInfo(ODBCHConnection connectionHandle, ushort infoType, out String resultStr)
        {
            short  _bufferLength    = 0;
            short  _stringLengthPtr = 0;
            IntPtr Value            = IntPtr.Zero;

            resultStr = String.Empty;
            var result = ODBCNativeMethods.SQLGetInfo(connectionHandle, infoType, Value, _bufferLength, out _stringLengthPtr);

            if ((result == ODBCResult.Success) || (result == ODBCResult.SuccessWithInfo))
            {
                if (_stringLengthPtr > 0)
                {
                    _bufferLength = _stringLengthPtr;
                    Value         = Marshal.AllocHGlobal(_stringLengthPtr);
                    result        = ODBCNativeMethods.SQLGetInfo(connectionHandle, infoType, Value, _bufferLength, out _stringLengthPtr);
                    if ((result == ODBCResult.Success) || (result == ODBCResult.SuccessWithInfo))
                    {
                        resultStr = Marshal.PtrToStringAnsi(Value);
                    }
                    else
                    {
                        Marshal.FreeHGlobal(Value);
                        var exerr = GetException(connectionHandle, string.Format("Unable to get information  about data source code '{0}'.", infoType.ToString()));
                        throw exerr;
                    }
                    Marshal.FreeHGlobal(Value);
                }

                return(true);
            }
            var ex = GetException(connectionHandle, string.Format("Unable to get information  about data source code '{0}'.", infoType.ToString()));

            throw ex;
        }
Пример #5
0
        internal static bool GetConnectionInfoString(ODBCHConnection connectionHandle, ushort infoType, out StringBuilder resultStr)
        {
            resultStr = new StringBuilder();
            short _bufferLength    = 0;
            short _stringLengthPtr = 0;
            var   result           = ODBCNativeMethods.SQLGetInfo(connectionHandle, infoType, null, _bufferLength, out _stringLengthPtr);

            if ((result == ODBCResult.Success) || (result == ODBCResult.SuccessWithInfo))
            {
                if (_stringLengthPtr > 0)
                {
                    resultStr.Capacity = _stringLengthPtr;
                    _bufferLength      = _stringLengthPtr;
                    result             = ODBCNativeMethods.SQLGetInfo(connectionHandle, infoType, resultStr, _bufferLength, out _stringLengthPtr);
                    if ((result != ODBCResult.Success) && (result != ODBCResult.SuccessWithInfo))
                    {
                        var ex = GetException(connectionHandle, string.Format("Unable to get information  about data source code '{0}'.", infoType.ToString()));
                        throw ex;
                    }
                }
            }
            else
            {
                var ex = GetException(connectionHandle, string.Format("Unable to get information  about data source code '{0}'.", infoType.ToString()));
                throw ex;
            }
            return(true);
        }
Пример #6
0
        public static bool SetConnectionProp(ODBCHConnection nStatementHandle, short cSetting, int eExpression)
        {
            var result = ODBCNativeMethods.SQLSetConnectAttr(nStatementHandle, cSetting, eExpression, 0);

            if (result != ODBCResult.Success)
            {
                throw GetException(nStatementHandle, "Unnable to set connection property ");
            }
            return(true);
        }
Пример #7
0
        public static bool DescribeCol(ODBCHStatement nStatementHandle, short ColumnNumber, ref String ColumnName, short BufferLength, out short NameLengthPtr, out short DataTypePtr, out int ColumnSizePtr, out short DecimalDigitsPtr, out short NullablePtr)
        {
            var result = ODBCNativeMethods.SQLDescribeCol(nStatementHandle, ColumnNumber, ref ColumnName, BufferLength, out NameLengthPtr, out DataTypePtr, out ColumnSizePtr, out DecimalDigitsPtr, out NullablePtr);

            if (result != ODBCResult.Success)
            {
                throw GetException(nStatementHandle, "Unnable to set connection property ");
            }
            return(true);
        }
Пример #8
0
        internal static bool GetForeignKeys(ODBCHStatement nStatementHandle, string PKCatalogName, string PKSchemaName, string PKTableName, string FKCatalogName, string FKSchemaName, string FKTableName)
        {
            var result = ODBCNativeMethods.SQLForeignKeys(nStatementHandle, PKCatalogName, (short)PKCatalogName.Length, PKSchemaName, (short)PKSchemaName.Length, PKTableName, (short)PKTableName.Length, FKCatalogName, (short)FKCatalogName.Length, FKSchemaName, (short)FKSchemaName.Length, FKTableName, (short)FKTableName.Length);

            if (result != ODBCResult.Success)
            {
                throw GetException(nStatementHandle, "Unnable to set connection property ");
            }
            return(true);
        }
Пример #9
0
        internal static bool Fetch(ODBCHStatement statementHandle)
        {
            var result = ODBCNativeMethods.SQLFetch(statementHandle);

            if ((result != ODBCResult.Success) & (result != ODBCResult.SuccessWithInfo) & (result != ODBCResult.NoData))
            {
                throw GetException(statementHandle, "Error fetching data");
            }
            return((result == ODBCResult.Success) || (result == ODBCResult.SuccessWithInfo));
        }
Пример #10
0
        internal static bool GetIndexInfo(ODBCHStatement statementHandle, string schemaName, string tableName, ushort indexType)
        {
            var result = ODBCNativeMethods.SQLStatistics(statementHandle, String.Empty, (short)0, schemaName, (short)schemaName.Length, tableName, (short)tableName.Length, indexType, 0);

            if ((result != ODBCResult.Success) & (result != ODBCResult.SuccessWithInfo))
            {
                throw GetException(statementHandle, "Error getting index definition");
            }
            return(true);
        }
Пример #11
0
        internal static bool GetPrimKey(ODBCHStatement statementHandle, string schemaName, string tableName)
        {
            var result = ODBCNativeMethods.SQLPrimaryKeys(statementHandle, String.Empty, (short)0, schemaName, (short)schemaName.Length, tableName, (short)tableName.Length);

            if ((result != ODBCResult.Success) & (result != ODBCResult.SuccessWithInfo))
            {
                throw GetException(statementHandle, "Error getting primary key definition");
            }
            return(true);
        }
Пример #12
0
        internal static bool GetTables(ODBCHStatement statementHandle, string catalogName, string schemaName, string tableName)
        {
            var result = ODBCNativeMethods.SQLTables(statementHandle, catalogName, (short)(catalogName.Length), schemaName, (short)(schemaName.Length), tableName, (short)(tableName.Length), String.Empty, (short)0);

            if ((result != ODBCResult.Success) & (result != ODBCResult.SuccessWithInfo))
            {
                throw GetException(statementHandle, "Error getting table list");
            }
            return(true);
        }
Пример #13
0
        internal static bool SQLBindColumn(ODBCHStatement statementHandle, short ColumnNumber, ODBCDataType TargetType, StringBuilder TargetValue, int BufferLength, out int StrLen_or_ind)
        {
            var result = ODBCNativeMethods.SQLBindCol(statementHandle, ColumnNumber, TargetType, TargetValue, BufferLength, out StrLen_or_ind);

            if ((result != ODBCResult.Success) & (result != ODBCResult.SuccessWithInfo))
            {
                throw GetException(statementHandle, "Error binding columns");
            }
            return(true);
        }
Пример #14
0
        internal static bool SQLBindColumn(ODBCHStatement statementHandle, ODBCData column)
        {
            var result = ODBCNativeMethods.SQLBindCol(statementHandle, column.ColumnNumber, column.ColumnType, column.ColumnData, column.ColumnDataLength, out column.ColumnDataRealLength);

            if ((result != ODBCResult.Success) & (result != ODBCResult.SuccessWithInfo))
            {
                throw GetException(statementHandle, "Error binding columns");
            }
            return(true);
        }
Пример #15
0
        internal static IntPtr AllocateEnvironmentHandle()
        {
            IntPtr handle;
            var    result = ODBCNativeMethods.SQLAllocHandle(ODBCHType.Environment, IntPtr.Zero, out handle);

            if ((result != ODBCResult.Success) && (result != ODBCResult.SuccessWithInfo))
            {
                throw new ODBCAPIError("Unable to allocate ODBC environment handle.");
            }

            return(handle);
        }
Пример #16
0
 internal static void SetIntEnvironmentAttribute(ODBCHEnvironment environmentHandle, ODBCEnvAttr attribute, ODBCVersion value)
 {
     if (environmentHandle != null)
     {
         var result = ODBCNativeMethods.SQLSetEnvAttr(environmentHandle, attribute, value, 0);
         if ((result != ODBCResult.Success) && (result != ODBCResult.SuccessWithInfo))
         {
             var ex = GetException(environmentHandle, string.Format("Unable to set ODBC environment attribute '{0:G}'.", attribute));
             throw ex;
         }
     }
 }
Пример #17
0
        internal static bool AllocateHandle(ODBCHType handleType, ODBCHEnvironment inputHandle, out IntPtr outputHandle)
        {
            IntPtr handle;
            var    result = ODBCNativeMethods.SQLAllocHandle(handleType, inputHandle, out handle);

            outputHandle = handle;
            if ((result != ODBCResult.Success) & (result != ODBCResult.SuccessWithInfo))
            {
                throw GetException(inputHandle, "Error allocating connection handle");
            }
            return(true);
        }
Пример #18
0
 internal static void Disconnect(ODBCHConnection connectionHandle)
 {
     if (connectionHandle != null)
     {
         var result = ODBCNativeMethods.SQLDisconnect(connectionHandle);
         if ((result == ODBCResult.Success) || (result == ODBCResult.SuccessWithInfo))
         {
             return;
         }
         var ex = GetException(connectionHandle, "Unable to disconnect the database.");
         throw ex;
     }
 }
Пример #19
0
        internal static bool AllocateHandle(ODBCHType handleType, IntPtr inputHandle, out IntPtr outputHandle)
        {
            IntPtr handle = IntPtr.Zero;
            var    result = ODBCNativeMethods.SQLAllocHandle(handleType, inputHandle, out handle);

            outputHandle = handle;

            if ((result != ODBCResult.Success) & (result != ODBCResult.SuccessWithInfo))
            {
                throw new ODBCAPIError("Can't allocate environment handle ");
            }

            return(true);
        }
Пример #20
0
        internal static bool GetConnectionInfoShort(ODBCHConnection connectionHandle, ushort infoType, out ushort resultShort)
        {
            resultShort = 0;
            short _bufferLength    = 4;
            short _stringLengthPtr = 0;
            var   result           = ODBCNativeMethods.SQLGetInfo(connectionHandle, infoType, out resultShort, _bufferLength, out _stringLengthPtr);

            if ((result != ODBCResult.Success) && (result != ODBCResult.SuccessWithInfo))
            {
                var ex = GetException(connectionHandle, string.Format("Unable to get information  about data source code '{0}'.", infoType.ToString()));
                throw ex;
            }
            return(true);
        }
Пример #21
0
        internal static IntPtr AllocateStatementHandle(ODBCHConnection connectionHandle)
        {
            if (connectionHandle == null)
            {
                throw new ArgumentNullException("connectionHandle");
            }
            IntPtr handle;
            var    result = ODBCNativeMethods.SQLAllocHandle(ODBCHType.Statement, connectionHandle, out handle);

            if ((result == ODBCResult.Success) || (result == ODBCResult.SuccessWithInfo))
            {
                return(handle);
            }
            var ex = GetException(connectionHandle, "Unable to allocate ODBC statement handle.");

            throw ex;
        }
Пример #22
0
        internal static bool ConnectTo(ODBCHConnection connectionHandle, string connectionString, short driverParam = 0)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentNullException("Connection string is empty!");
            }

            short         bufferLengthNeeded;
            StringBuilder outString = new StringBuilder(1024);
            var           result    = ODBCNativeMethods.SQLDriverConnectW(connectionHandle, IntPtr.Zero, connectionString, (short)connectionString.Length, outString, (short)outString.Capacity, out bufferLengthNeeded, driverParam);

            if ((result != ODBCResult.Success) & (result != ODBCResult.SuccessWithInfo))
            {
                throw GetException(connectionHandle, "Unnable to connect using connection string " + connectionString);
            }
            return(true);
        }
Пример #23
0
 internal static bool ReleaseHandle(ODBCHType handleType, IntPtr handle)
 {
     return(ODBCNativeMethods.SQLFreeHandle(handleType, handle) == ODBCResult.Success);
 }