public class AddLocalDSN
{
public AddLocalDSN(string intDSName, string intDBPath)
{
bool addDSNSuccess = CreateDataSource((IntPtr)0,
4,
"Microsoft Access Driver (*.MDB)\0",
"DSN=" + intDSName + "\0Uid=Admin\0pwd=\0DBQ=" +
intDBPath + "\0");
}
/// <summary>
/// Win32 API Imports
/// </summary>
[DllImport( "ODBCCP32.dll")]
private static extern bool SQLConfigDataSource(IntPtr hwndParent,
int fRequest, string lpszDriver, string lpszAttributes);
public static bool CreateDataSource(IntPtr hwndParent,
int fRequest,
string lpszDriver,
string lpszAttributes)
{
return SQLConfigDataSource(hwndParent,
fRequest,
lpszDriver,
lpszAttributes);
}
}
Note:
The int 4 refers to the value "ODBC_ADD_SYS_DSN". 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_DSN (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*************@TK2MSFTNGP12.phx.gbl...
I'm trying to find some code on how to create a DSN using C# and
SQLConfigDataSource.
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!