[DllImport("ODBCCP32.DLL",CharSet=CharSet.Unicode, SetLastError=true)]
static extern int SQLConfigDataSource (int hwndParent, int fRequest, string lpszDriver, string lpszAttributes);
<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
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
http://support.microsoft.com/kb/126606/EN-US/
lpszAttributes:
Supports Name value pairs.
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.
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);
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
Do you know one? Please contribute it!