By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,158 Members | 917 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,158 IT Pros & Developers. It's quick & easy.

Create DSN

P: n/a
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!
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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!

Nov 16 '05 #2

P: n/a

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!
Nov 16 '05 #3

P: n/a
Tried it and works fine

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Stevie" <St****@devdex.com> wrote in message
news:O$****************@tk2msftngp13.phx.gbl...

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!

Nov 16 '05 #4

P: n/a

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!
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.