sqlconfigdatasource (odbccp32)
Last changed: http---joshuanorton.spaces.live.com-210.54.237.168

.
Summary
This function is used to work with the ODBC Datasources

C# Signature:

[DllImport("ODBCCP32.DLL",CharSet=CharSet.Unicode, SetLastError=true)]
static extern int SQLConfigDataSource (int hwndParent, int fRequest, string lpszDriver, string lpszAttributes);

VB Signature:

<DllImport("ODBCCP32.dll",CallingConvention:=CallingConvention.WinAPI,CharSet:=CharSet.Unicode,SetLastError:=True)> _
Public Shared Function SQLConfigDataSource(ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer

User-Defined Types:

fRequest:
ODBC_ADD_DSN
ODBC_CONFIG_DSN
ODBC_REMOVE_DSN
ODBC_ADD_SYS_DSN
ODBC_CONFIG_SYS_DSN
ODBC_REMOVE_SYS_DSN
ODBC_REMOVE_DEFAULT_DSN

More

http://support.microsoft.com/kb/126606/EN-US/

lpszAttributes:

Supports Name value pairs.

More

Notes:

None.

Tips & Tricks:

Constructing the attrubute strings be sure to put quotes around the file name. Otherwise,

if there are spaces in the file name the function call wil fail.

EG:
String.Format("CREATE_DBV4=\"{0}\" General\0", FileName);

Sample Code:

C#

public static bool CreateSQLDSN(string dsnName, string  serverName, string dbName)
{
    string Driver;
    string attribs;
    Driver = "SQL Server";
    attribs = "SERVER=" + serverName + Convert.ToChar(0);
    attribs = attribs + "DESCRIPTION=Test DSN" + Convert.ToChar(0);
    attribs = attribs + "DSN=" + dsnName + Convert.ToChar(0);
    attribs = attribs + "DATABASE=" + dbName  + Convert.ToChar(0);
    attribs = attribs + "Trusted_Connection=yes"  + Convert.ToChar(0);
    int intRet = win32apis.SQLConfigDataSource(0, win32apis.ODBC_ADD_DSN, Driver, attribs);
    if (intRet == 1)
        return true;
    return false;
}

internal class win32apis
{
    internal const int ODBC_ADD_DSN = 1;    
    [DllImport("ODBCCP32.DLL",CharSet=CharSet.Unicode)]
    internal extern static int SQLConfigDataSource (int hwndParent, int fRequest, string lpszDriver, string lpszAttributes);
}

VB

Imports System
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Private Enum ODBC_Constants As Integer
    ODBC_ADD_DSN = 1
    ODBC_CONFIG_DSN
    ODBC_REMOVE_DSN
    ODBC_ADD_SYS_DSN
    ODBC_CONFIG_SYS_DSN
    ODBC_REMOVE_SYS_DSN
    ODBC_REMOVE_DEFAULT_DSN
End Enum

<DllImport("ODBCCP32.dll",CallingConvention:=CallingConvention.WinAPI,CharSet:=CharSet.Unicode,SetLastError:=True)> _
Private Shared Function SQLConfigDataSource(ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer

Private Function CreateSystemDSN() As Boolean
    Dim vAttributes As String = "DSN=My DSN Name" & Convert.ToChar(0)
    vAttributes &= "Description=My DSN Description" & Convert.ToChar(0)
    vAttributes &= "Trusted_Connection=Yes" & Convert.ToChar(0)
    vAttributes &= "Server=SQLSERVERINSTANCE" & Convert.ToChar(0)
    vAttributes &= "Database=MyDatabaseName" & Convert.ToChar(0)

    If SQLConfigDataSource(0, ODBC_Constants.ODBC_ADD_SYS_DSN, "SQL Server", vAttributes) = 0 Then
        Messagebox.Show("Failed to create ODBC data source!!")
        Return False
    End If
    Return True
End Function

Alternative Managed API:

Do you know one? Please contribute it!

Documentation