Solved

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

Posted on 2004-03-28
5
2,831 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:mvarghese
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10698008
>> 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
0
 

Author Comment

by:mvarghese
ID: 10701182
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
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 10701454
Windows handler should be passes as IntPtr, NULL is IntPtr.Zero. Two last parameters are LPCSTR, this means, use [MarshalAs(UnmanagedType.LPStr)] for them.

0
 

Author Comment

by:mvarghese
ID: 10702116
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
0
 

Expert Comment

by:snafder
ID: 25800059
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now