Solved

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

Posted on 2004-03-28
5
2,881 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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