Desktop Functions:

   Smart Device Functions:

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

SQLConfigDataSource (odbccp32)
This function is used to work with the ODBC Datasources

C# Signature:

[DllImport("ODBCCP32.DLL", CharSet=CharSet.Ansi)]
static extern bool SQLConfigDataSource(
    IntPtr parent,
    short request,
    string driver,
    string attributes);

VB Signature:

Private Declare Function SQLConfigDataSource Lib "ODBCCP32.dll"
    ( ByVal hwndParent As IntPtr,
      ByVal fRequest As Integer,
      ByVal lpszDriver As String,
      ByVal lpszAttributes As String) As Boolean

User-Defined Types:



[2004-10-19 - Nicholas Paldino]
Fixed the declaration (from int to short), as well as cleaned up incorrect code.

Tips & Tricks:

Try out this class to create User and System DSNs:

Public Class LocalDSN

    ' Enum

    Public Enum ADD_TYPE
    USER = 1
    SYSTEM = 4
    End Enum

    Public Enum REMOVE_TYPE
    USER = 3
    SYSTEM = 6
    End Enum

    ' Constants

    Private Const ODBC_ADD_DSN As Integer = 1 '(use this to add a user DSN)
    Private Const ODBC_CONFIG_DSN As Integer = 2 '(use this to configure a user DSN)
    Private Const ODBC_REMOVE_DSN As Integer = 3 '(use this to remove a user DSN)
    Private Const ODBC_ADD_SYS_DSN As Integer = 4 '(use this to add a system DSN)
    Private Const ODBC_CONFIG_SYS_DSN As Integer = 5 '(use this to configure a system DSN)
    Private Const ODBC_REMOVE_SYS_DSN As Integer = 6 '(use this to remove a system DSN)

    ' Ingres

    ' Attributes

    ' BlankDate (NULL or blank)
    ' CatConnect (Y or N)
    ' CatSchemaNULL (Y or N)
    ' ConvertThreePartNames (Y or N)
    ' Database
    ' Date1582 (NULL or blank)
    ' Description
    ' Driver
    ' Numeric_overflow (Ignore or blank)
    ' Prompt(Y or N)
    ' RoleName
    ' RolePWD
    ' SelectLoops (Y or N)
    ' Server
    ' ServerDelimitedCase
    ' ServerMaxColumnNameLen
    ' ServerMaxColumns
    ' ServerMaxSchemaNameLen
    ' ServerMaxTableNameLen
    ' ServerNameCase
    ' ServerType
    ' ServerVersion
    ' SupportIIDECIMAL (Y or N)
    ' UseSysTables (Y or N)
    ' WithOption (Refers to Enterprise Gateway with Options)

    ' Create/Configure

    Public Shared Function CreateIngresDB_DSN(ByVal DSName As String, ByVal description As String, ByVal server As String, ByVal db As String, ByVal type As ADD_TYPE, ByVal prompt As Boolean) As Boolean
    Dim promptStr As String = "N"
    Dim odbc_const As Integer = 0
    Dim reg As Microsoft.Win32.RegistryKey

    If prompt Then
        promptStr = "Y"
    End If

    Select Case type
        Case ADD_TYPE.USER
        reg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\ODBC\ODBC.INI\" & DSName)

        Case ADD_TYPE.SYSTEM
        reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\ODBC\ODBC.INI\" & DSName)

    End Select

    If reg Is Nothing Then
        odbc_const = type
        Select Case type
        Case ADD_TYPE.USER
            odbc_const = ODBC_CONFIG_DSN

        Case ADD_TYPE.SYSTEM
            odbc_const = ODBC_CONFIG_SYS_DSN

        End Select

    End If

    Return DoDataSource(New IntPtr(0), _
           odbc_const, _
           "Ingres", _
           "DSN=" & DSName & Chr(0) & "SERVER=" & server & Chr(0) & "DB=" & db & Chr(0) & "PROMPT=" & promptStr & Chr(0) & "DESCRIPTION=" & description & Chr(0))

    End Function

    ' Remove Ingres

    Public Shared Function RemoveIngresDB_DSN(ByVal DSName As String, ByVal type As REMOVE_TYPE) As Boolean
    Return DoDataSource(New IntPtr(0), _
        type, _
        "Ingres", _
        "DSN=" & DSName & Chr(0))

    End Function

    ' MS Access

    Public Shared Function CreateAccessDB_DSN(ByVal DSName As String, ByVal DBPath As String) As Boolean
    Return DoDataSource(New IntPtr(0), _
        4, _
        "Microsoft Access Driver (*.MDB)", _
        "DSN=" & DSName & ";Uid=Admin;pwd=;DBQ=" & DBPath & ";")

    End Function

    ' ODBC Api Call

    Private Declare Function SQLConfigDataSource Lib "ODBCCP32.dll" (ByVal hwndParent As IntPtr, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Boolean

    Private Shared Function DoDataSource(ByVal hwndParent As IntPtr, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Boolean
    Return SQLConfigDataSource(hwndParent, fRequest, lpszDriver, lpszAttributes)
    End Function

End Class

Sample Code:


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