creating odbc dsn with c#

  • Thread starter Thread starter Andi Reisenhofer
  • Start date Start date
A

Andi Reisenhofer

Hallo C# folks,

Somebody know how to create a ODBC DSN dynamically in c# program.
Also interesting for me would be the connectionstring for an Access
Database.

Thinks a lot
Andreas
 
Andi,

In order to create a DSN, you will probably have to call
SQLWriteDSNToIni through the P/Invoke layer in ODBC.dll.

Hope this helps.
 
Hallo Nicholas,

I found this one also now: SQLConfigDataSource
Do you think I could use this one also?
It seems this one can produce also System and User Dsn's

Have you an idea how I must declare it in c#?
Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As
Long, _
ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As
String) As Long


Thanks.
Andreas

Andi,

In order to create a DSN, you will probably have to call
SQLWriteDSNToIni through the P/Invoke layer in ODBC.dll.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andi Reisenhofer said:
Hallo C# folks,

Somebody know how to create a ODBC DSN dynamically in c# program.
Also interesting for me would be the connectionstring for an Access
Database.

Thinks a lot
Andreas
 
I am also interested in a detailed description of this... I have read everything I can find on the SQLConfigDataSource function. Does anyone know how to implement this within C#.net - (i.e. add a MS Access DSN to System DSNs)? Thanks!
-Lewt

Andi Reisenhofer said:
Hallo Nicholas,

I found this one also now: SQLConfigDataSource
Do you think I could use this one also?
It seems this one can produce also System and User Dsn's

Have you an idea how I must declare it in c#?
Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As
Long, _
ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As
String) As Long


Thanks.
Andreas

Andi,

In order to create a DSN, you will probably have to call
SQLWriteDSNToIni through the P/Invoke layer in ODBC.dll.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andi Reisenhofer said:
Hallo C# folks,

Somebody know how to create a ODBC DSN dynamically in c# program.
Also interesting for me would be the connectionstring for an Access
Database.

Thinks a lot
Andreas
 
Have you looked at OLEDb rather than ODBC?
To get to your access database you could establish a connection via;
string vConStr = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=C:\\Testdb.mdb;"
+ "User ID=Admin;Password=MyPassword;" // optional
+ "Jet OLEDB:Database Password=DevId"; //optional
System.Data.OleDb.OleDbConnection vDatabase = new OleDbConnection(vConStr);
vDatabase.Open();
The ODBC and OLEDb methods are very similar from this point on.

OR

If you already have the MSAccess ODBC drivers installed you do not need to
create a specific DSN for your individual connection. eg,
string vConStr = "Driver={Microsoft Access Driver (*.mdb)};"
+ "DBQ=C:\\Testdb.mdb;UID=;PWD=;";
System.Data.Odbc.OdbcConnection vDatabase = new OdbcConnection(vConStr);


- Colin.


Lewt said:
I am also interested in a detailed description of this... I have read
everything I can find on the SQLConfigDataSource function. Does anyone know
how to implement this within C#.net - (i.e. add a MS Access DSN to System
DSNs)? Thanks!
-Lewt

Andi Reisenhofer said:
Hallo Nicholas,

I found this one also now: SQLConfigDataSource
Do you think I could use this one also?
It seems this one can produce also System and User Dsn's

Have you an idea how I must declare it in c#?
Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As
Long, _
ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As
String) As Long


Thanks.
Andreas

Andi,

In order to create a DSN, you will probably have to call
SQLWriteDSNToIni through the P/Invoke layer in ODBC.dll.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hallo C# folks,

Somebody know how to create a ODBC DSN dynamically in c# program.
Also interesting for me would be the connectionstring for an Access
Database.

Thinks a lot
Andreas
 
Colin, thanks for your quick response. I know all about the OLE stuff. Yes, it works great. Unfortunately, some of us don't have the liberty of being able to utilize that.

I am creating a deployment project in which I do not have control over the source code for the original application. Unfortunately, for me, this app uses ODBC DSNs to connect to multiple access databases. This means that on the target computer I need the ability to create those DSNs - since asking the user to figure out the ODBC Administrator is way too much to ask of the users these days.

Please pardon me if I'm laying it on thick, but I've been scowering the Internet looking for this missing link (between C# & the .NET framework and this MFC? SQLConfigDataSoure function), but all I can find is several people asking and responses of alternative methodologies.

I know it can be done - there is a company out there who has done it and is selling the code. Unfortunately, I paid them the money and they ave not delivered the code - legal proceedings may be forthcoming. In the meantime, does anyone know how to do this? Thanks!!

-Lewt
 
I now see your dilemma.

Not sure of the appropiate API calls, but an alternative may be to simple
add the appropiate registry entries for the DSN. I would try;
1) Export your registry to a disk file,
2) Create a new DSN with your desired properties via the control panel
applet,
3) Export your registry to a second disk file
Comparing the 2 export images should give you a snapshot of what registry
entries are being created, which you may then be able to insert via you .Net
app. Not "by the book" Im sure, but it may prove just as effective.

- Colin

Lewt said:
Colin, thanks for your quick response. I know all about the OLE stuff.
Yes, it works great. Unfortunately, some of us don't have the liberty of
being able to utilize that.
I am creating a deployment project in which I do not have control over the
source code for the original application. Unfortunately, for me, this app
uses ODBC DSNs to connect to multiple access databases. This means that on
the target computer I need the ability to create those DSNs - since asking
the user to figure out the ODBC Administrator is way too much to ask of the
users these days.
Please pardon me if I'm laying it on thick, but I've been scowering the
Internet looking for this missing link (between C# & the .NET framework and
this MFC? SQLConfigDataSoure function), but all I can find is several people
asking and responses of alternative methodologies.
I know it can be done - there is a company out there who has done it and
is selling the code. Unfortunately, I paid them the money and they ave not
delivered the code - legal proceedings may be forthcoming. In the meantime,
does anyone know how to do this? Thanks!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top