Link to home
Start Free TrialLog in
Avatar of mvarghese
mvarghese

asked on

How to create PROGRAMMATICALLY DSN CONNECTION USING C# (VERY URGENT)

I would like to create a "DSN programmatically using C#". It was easy to create the DSN (System DSN) using the API's [SQLConfigDataSource] in VB 6.0. When trying to create the same in C# it is not creating. Will anyone please help me out for the same. I want to create the DSN for MS Access and MS SQL Server. Below is the API used in VB 6.0.

Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
   (ByVal hwndParent As Long, ByVal fRequest As Long, _
   ByVal lpszDriver As String, ByVal lpszAttributes As String) _
   As Long

Private Const ODBC_ADD_SYS_DSN = 4

Will any please help me out its really very urgent.
Thanks,
Mathew
Avatar of AlexFM
AlexFM

>> When trying to create the same in C# it is not creating.

Can you show your code? Take a look also at this thread:

http://www.devcity.net/forums/topic.asp?tid=5341
Avatar of mvarghese

ASKER

Hi AlexFM,
Please see the code which i have used

[DllImport("ODBCCP32.DLL") ]
static extern long SQLConfigDataSource (long hwndParent, long fRequest, string lpszDriver, string lpszAttributes);

and used this api defination as:
intRet = SQLConfigDataSource(0&, 1, strDriver, sAttributes);

Not getting the alternative for the first parameter 0& (null in VB).

please help me out.
Thanks,
Mathew
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks AlexFM.

I have solved this problem by defining the API as

[DllImport("ODBCCP32.DLL", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Ansi, ExactSpelling=true) ]
static extern bool SQLConfigDataSource (int hwndParent, int fRequest, string lpszDriver, string lpszAttributes);

the solution you have provided is also fine.

Thanks a lot,
Mathew
Hi mvarghese (Mathew),

Can you provide the entire C# code for the solution?

"Thanks AlexFM.

I have solved this problem by defining the API as

[DllImport("ODBCCP32.DLL", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Ansi, ExactSpelling=true) ]
static extern bool SQLConfigDataSource (int hwndParent, int fRequest, string lpszDriver, string lpszAttributes);

the solution you have provided is also fine.

Thanks a lot,
Mathew"
Cheers,
Snafder