public void Initialize(ODBCVersion version = ODBCVersion.Version2)//по умолчанию используем ODBC 2.0, т.к. работаем с устаревшими БД { Close(); _enviromentHandle = new ODBCHEnvironment(version); _connectionHandle = new ODBCHConnection(_enviromentHandle); _initialized = true; }
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; }
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); }
public bool SetConnectionProperty(ODBCHConnection nStatementHandle, short prop, int val) { if (_initialized) { return(ODBCNative.ODBCMethods.SetConnectionProp(nStatementHandle, prop, val)); } return(false); }
internal static extern ODBCResult SQLDriverConnectW( ODBCHConnection connectionHandle, IntPtr windowHandle, string inConnectionString, short inConnectionStringLength, StringBuilder outConnectionStringBuffer, short bufferLength, out short bufferLengthNeeded, short fDriverCompletion);
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); }
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; } }
internal static bool AllocateHandle(ODBCHType handleType, ODBCHConnection 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 statement handle"); } return(true); }
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); }
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); }
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; }
private void Close() { if (_connected) { try { ODBCNative.ODBCMethods.Disconnect(_connectionHandle); } catch (ODBCAPIError e) { _connected = false; #if DEBUG System.Diagnostics.Trace.Write(e.Message); #endif } } try { if (_connectionHandle != null) { _connectionHandle.Dispose(); } if (_enviromentHandle != null) { _enviromentHandle.Dispose(); } } catch { _initialized = false; } _connectionHandle = null; _enviromentHandle = null; _connected = false; _initialized = false; }
internal static extern ODBCResult SQLDisconnect( ODBCHConnection connectionHandle);
internal static extern ODBCResult SQLAllocHandle( ODBCHType handleType, ODBCHConnection inputHandle, out IntPtr outputHandle);
//установка атрибутов соединения //строковое значение атрибута public static extern ODBCResult SQLSetConnectAttr( ODBCHConnection nStatementHandle, int Attribute, string ValuePtr, int eExpression );
//информация о драйвере и об источнике данных выбранного соединения //прототип для для целочисленной информации public static extern ODBCResult SQLGetInfo( ODBCHConnection connectionHandle, ushort InfoType, out ushort InfoValue, short BufferLength, out short StringLengthPtr);