Create DSN

  • Thread starter Thread starter Steve Sweales
  • Start date Start date
S

Steve Sweales

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!
 
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;
 
Thats the same post as I have found, but I can't seem to get it to wrk
for SQL Server.

It always returns a false.


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
I can get it to work as it is (for Access), but I can not get it to
create a DSN for SQL Server.

Any ideas?


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Back
Top