Desktop Functions:

   Smart Device Functions:

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

Terms of Use
Privacy Policy
sqldriverconnect (odbc32)
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:

    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

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_COMPLETE_REQUIRED = 3;

    //return values
    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_INVALID_HANDLE      = (-2);



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;


        if (!isOK(SQLDriverConnect(connectionHandle,
            (short) in_connect.Length,
            out len,
                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,
            out native_error,
            (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)
        if (connectionHandle.ToInt32()!=0)
            connectionHandle = IntPtr.Zero;
        if (environmentHandle.ToInt32()!=0)
            environmentHandle = IntPtr.Zero;
        isAllocated = false;

Alternative Managed API:

Do you know one? Please contribute it!


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 directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version