public class AddLocalDSN
{
public AddLocalDSN(str ing intDSName, string intDBPath)
{
bool addDSNSuccess = CreateDataSourc e((IntPtr)0,
4,
"Microsoft Access Driver (*.MDB)\0",
"DSN=" + intDSName + "\0Uid=Admin\0p wd=\0DBQ=" +
intDBPath + "\0");
}
/// <summary>
/// Win32 API Imports
/// </summary>
[DllImport( "ODBCCP32.d ll")]
private static extern bool SQLConfigDataSo urce(IntPtr hwndParent,
int fRequest, string lpszDriver, string lpszAttributes) ;
public static bool CreateDataSourc e(IntPtr hwndParent,
int fRequest,
string lpszDriver,
string lpszAttributes)
{
return SQLConfigDataSo urce(hwndParent ,
fRequest,
lpszDriver,
lpszAttributes) ;
}
}
Note:
The int 4 refers to the value "ODBC_ADD_SYS_D SN". To remove a system DSN use
the int 6. See below:
1 - ODBC_ADD_DSN (use this to add a user DSN)
2 - ODBC_CONFIG_DSN (use this to configure a user DSN)
3 - ODBC_REMOVE_DSN (use this to remove a user DSN)
4 - ODBC_ADD_SYS_DS N (use this to add a system DSN)
5 - ODBC_CONFIG_SYS _DSN (use this to configure a system DSN)
6 - ODBC_REMOVE_SYS _DSN (use this to remove a system DSN)
Also, you must include the following using reference:
using System.Runtime. InteropServices ;
--
Tamir Khason
You want dot.NET? Just ask:
"Please,
www.dotnet.us "
"Steve Sweales" <st***********@ t-mobile.co.uk> wrote in message
news:Of******** *****@TK2MSFTNG P12.phx.gbl...
I'm trying to find some code on how to create a DSN using C# and
SQLConfigDataSo urce.
Can anybody help me please, before I tear my hair out!!
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!