Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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