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

sqlconfigdatasource (odbccp32)
 
.
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:

One of the more popular uses for this is manipulating mdb database files. Thisi is the file format used by Microsoft Access. You can create, compact and repair mdb files with this function.

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 will fail.

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

Sample Code:

C#

This class allows you to Create, Compact, Repair and delete Microsoft Access Databases.

using System;
using System.Runtime.InteropServices;

namespace PInvoke
{
    /// <summary>
    /// JetSQL is the "code name" for the sql engine behind access.
    /// It's auctually built into windows. Microsoft Access is just a fancy
    /// front end.
    /// </summary>
    public class JetSqlUtil
    {
        internal enum ODBC_Constants : int {
             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,
        }

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


        /// <summary>
        /// Compacts an access database
        /// </summary>
        /// <param name="FileName">The name of the databse to compact.</param>
        public static void CompactMDB (string FileName) {
            int retCode;
            string Attributes =
                String.Format("COMPACT_DB=\"{0}\" \"{0}\" General\0", FileName);
            retCode = SQLConfigDataSource
                (0, ODBC_Constants.ODBC_ADD_DSN,
                "Microsoft Access Driver (*.mdb)", Attributes);
            if (retCode == 0) {
                throw new ApplicationException("Cannot compact database: " + FileName);
            }
        }


        /// <summary>
        /// Creates an access database
        /// </summary>
        /// <param name="FileName">The name of the databse to create.</param>
        public static void CreateMDB (string FileName) {
            int retCode;
            string Attributes =
                String.Format("CREATE_DB=\"{0}\" General\0", FileName);
            retCode = SQLConfigDataSource
                (0, ODBC_Constants.ODBC_ADD_DSN,
                "Microsoft Access Driver (*.mdb)", Attributes);
            if (retCode == 0) {
                throw new ApplicationException("Cannot create file: " + FileName);
            }
        }



        /// <summary>
        /// Repairs an access database
        /// </summary>
        /// <param name="FileName">The name of the databse to repair.</param>
        public static void RepairMDB (string FileName) {
            int retCode;
            string Attributes =
                String.Format("REPAIR_DB=\"{0}\"\0", FileName);
            retCode = SQLConfigDataSource
                (0, ODBC_Constants.ODBC_ADD_DSN,
                "Microsoft Access Driver (*.mdb)", Attributes);
            if (retCode == 0) {
                throw new ApplicationException("Cannot repair database: " + FileName);
            }
        }

    }
}


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

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