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

SQLDataSources (odbc32)
 
.
Summary
Provide access to configured user and system Data Source Names (DSN) and installed ODBC drivers

C# Signature:

/// <summary>
/// SQLDataSources returns information about a data source. This function is implemented solely by the Driver Manager.
/// </summary>
/// <param name="EnvironmentHandle">[Input] Environment handle.</param>
/// <param name="Direction">[Input] Determines which data source the Driver Manager returns information on.</param>
/// <param name="ServerName">[Output] Pointer to a buffer in which to return the data source name.</param>
/// <param name="BufferLength1">[Input] Length of the *ServerName buffer, in characters; this does not need to be longer than SQL_MAX_DSN_LENGTH plus the null-termination character.</param>
/// <param name="NameLength1Ptr">[Output] Pointer to a buffer in which to return the total number of bytes (excluding the null-termination byte)
/// available to return in *ServerName. If the number of bytes available to return is greater than or equal to BufferLength1, the data
/// source name in *ServerName is truncated to BufferLength1 minus the length of a null-termination character. </param>
/// <param name="Description">[Output] Pointer to a buffer in which to return the description of the driver associated with the data source.
/// For example, dBASE or SQL Server.</param>
/// <param name="BufferLength2">[Input] Length in characters of the *Description buffer.</param>
/// <param name="NameLength2Ptr">[Output] Pointer to a buffer in which to return the total number of bytes (excluding the null-termination
/// byte) available to return in *Description. If the number of bytes available to return is greater than or equal to BufferLength2,
/// the driver description in *Description is truncated to BufferLength2 minus the length of a null-termination character.</param>
/// <returns>SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_NO_DATA, SQL_ERROR, or SQL_INVALID_HANDLE.</returns>
[DllImport("odbc32.dll", CharSet=CharSet.Ansi)]
static extern short SQLDataSources(IntPtr EnvironmentHandle, short Direction,
    StringBuilder ServerName, short BufferLength1, ref short NameLength1Ptr,
    StringBuilder Description, short BufferLength2, ref short NameLength2Ptr);

VB Signature:

Declare Function SQLDataSources _
    Lib "odbc32.dll" _
    ( _
    ByVal EnvironmentHandle As Integer, _
    ByVal Direction As Short, _
    ByVal ServerName As String, _
    ByVal BufferLength1 As Short, _
    ByRef NameLength1Ptr As Short, _
    ByVal Description As String, _
    ByVal BufferLength2 As Short, _
    ByRef NameLength2Ptr As Short _
    ) _
    As Short

User-Defined Types:

None.

Notes:

MSDN Documentation was sourced for the precise parameter definitions and return values.

You must provide an Environment Handle provided by SQLAllocHandle as the first parameter. SQLAllocHandle is a replacement for SQLAllocEnv. When using SQLAllocHandle, you must call SQLSetEnvAttr and set the SQL_ATTR_ODBC_VERSION attribute to SQL_OV_ODBC3 before calling SQLDataSources.

Tips & Tricks:

Depending on the value of Direction, you may access System DSN, User DSN or both. Since the same DSN name may be used in User and System DSN, watch out for possible duplicate values.

Sample Code:

One use of this function is to populate a combobox with available DSN's in a login screen...

    private void GetODBC()
    {
        const int SQL_SUCCESS = 0;
        const int SQL_FETCH_NEXT = 1;
        int i = 0;
        StringBuilder sDSNItem;
        StringBuilder sDRVItem;
        string sDSN = string.Empty;
        string sDRV = string.Empty;
        short iDSNLen = 0;
        short iDRVLen = 0;
        IntPtr lHenv ; //     'handle to the environment

        //get the DSNs
        if ( NativeWIN32.SQLAllocEnv(out lHenv) != -1)
        {
            do
            {
                sDSNItem = new StringBuilder(1024);
                       sDRVItem = new StringBuilder(1024);
                i = NativeWIN32.SQLDataSources(lHenv, SQL_FETCH_NEXT, sDSNItem, 1024, ref iDSNLen, sDRVItem, 1024, ref iDRVLen);
                if (i == SQL_SUCCESS)
                {
                    sDSN = sDSNItem.ToString().Substring(0, iDSNLen);
                    sDRV = sDRVItem.ToString().Substring(0, iDRVLen);
                }  
                }
            while(i == SQL_SUCCESS);
        }
    }

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
Find References
Show Printable Version
Revisions