?
Solved

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

Posted on 2004-03-28
5
Medium Priority
?
2,930 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 375 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

752 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