Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SQLDriverConnect (odbc32)
 
.
Summary
SQLDriverConnect is an alternative API to SQLConnect. The difference is that it can popup the dialog to ask the user for more information about the connection

C# Signature:

    [DllImport("odbc32.dll",CharSet=CharSet.Ansi)]
    public static extern short SQLDriverConnect(IntPtr  hdbc,
        IntPtr    hwnd,
        string          szConnStrIn,
        short     cbConnStrIn,
        StringBuilder szConnStrOut,
        short     cbConnStrOutMax,
        out short     pcbConnStrOut,
        ushort    fDriverCompletion);

VB Signature:

<Runtime.InteropServices.DllImport("odbc32.dll", CharSet:=Runtime.InteropServices.CharSet.Unicode)> _
    Private Shared Function SQLDriverConnect(ByVal hdbc As IntPtr, ByVal hwnd As IntPtr, ByVal szConnStrIn As String, _
    ByVal cbConnStrIn As Short, ByVal szConnStrOut As Text.StringBuilder, ByVal cbConnStrOutMax As Short, _
    <Runtime.InteropServices.Out()> ByRef pbcConnStrOut As Short, ByVal fDriverCompletion As Short) As Short
    End Function

TODO

User-Defined Types:

    //driver completion modes
    public const ushort SQL_DRIVER_NOPROMPT          = 0;
    public const ushort SQL_DRIVER_COMPLETE          = 1;
    public const ushort SQL_DRIVER_PROMPT            = 2;
    public const ushort SQL_DRIVER_NOPROMPT      = 0;
    public const ushort SQL_DRIVER_COMPLETE      = 1;
    public const ushort SQL_DRIVER_PROMPT        = 2;
    public const ushort SQL_DRIVER_COMPLETE_REQUIRED = 3;

    //return values
    public const short SQL_SUCCESS             =   0;
    public const short SQL_SUCCESS         =   0;
    public const short SQL_SUCCESS_WITH_INFO   =   1;    
    public const short SQL_STILL_EXECUTING     =   2;
    public const short SQL_NEED_DATA           =  99;
    public const short SQL_NO_DATA             = 100;
    public const short SQL_ERROR               = (-1);
    public const short SQL_NEED_DATA       =  99;
    public const short SQL_NO_DATA         = 100;
    public const short SQL_ERROR           = (-1);
    public const short SQL_INVALID_HANDLE      = (-2);

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    void driverConnect(string connstr, ushort driverCompletion)
    {
        const short    MAX_CONNECT_LEN = 1024;
        StringBuilder  out_connect = new StringBuilder(MAX_CONNECT_LEN);
        string            in_connect  = connstr;
        short      len = 0;

        init();

        if (!isOK(SQLDriverConnect(connectionHandle,
            GetActiveWindow(),
            in_connect,
            (short) in_connect.Length,
            out_connect,
            MAX_CONNECT_LEN,
            out len,
            driverCompletion)))
            {
                string msg  = GetError(IntPtr.Zero) + "\nconnection string:\n\t" + connstr;
                throw new Exception(msg);
            }
    }

    static bool isOK(int ret)
    {
        return ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO;
    }

    string GetError(IntPtr stmt)
    {
        StringBuilder state = new StringBuilder(256);
        StringBuilder msg   = new StringBuilder(256);

        int  native_error;
        short error_size;

        string lastError="";
        if (isOK(SQLError(environmentHandle,
            connectionHandle,
            stmt,
            state,
            out native_error,
            msg,
            (short) msg.Capacity,
            out error_size)))
        {
            string err = msg.ToString();
            int bracketPos = err.LastIndexOf("]");
            lastError = err.Substring(bracketPos+1).Trim();
        }
        return lastError;
    }

    void init()
    {
        if (!isAllocated)
        {
            if (!isOK(SQLAllocEnv(out environmentHandle)))
            {
                throw new Exception("Failed to allocate environment handle.");
            }
            if (!isOK(SQLAllocConnect(environmentHandle, out connectionHandle)))
            {
                throw new Exception("Failed to allocate connection handle.");
            }
            isAllocated = true;
        }
    }

    void terminate()
    {
        if (connectionHandle.ToInt32()!=0)
        {
            SQLDisconnect(connectionHandle);
        }
        if (connectionHandle.ToInt32()!=0)
        {
            SQLFreeConnect(connectionHandle);
            connectionHandle = IntPtr.Zero;
        }
        if (environmentHandle.ToInt32()!=0)
        {
            SQLFreeEnv(environmentHandle);
            environmentHandle = IntPtr.Zero;
        }
        isAllocated = false;
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions