473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Create DSN

I'm trying to find some code on how to create a DSN using C# and
SQLConfigDataSource.

Can anybody help me please, before I tear my hair out!!

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
4 11582
public class AddLocalDSN
{
public AddLocalDSN(string intDSName, string intDBPath)
{
bool addDSNSuccess = CreateDataSource((IntPtr)0,
4,
"Microsoft Access Driver (*.MDB)\0",
"DSN=" + intDSName + "\0Uid=Admin\0pwd=\0DBQ=" +
intDBPath + "\0");
}
/// <summary>
/// Win32 API Imports
/// </summary>
[DllImport( "ODBCCP32.dll")]
private static extern bool SQLConfigDataSource(IntPtr hwndParent,
int fRequest, string lpszDriver, string lpszAttributes);

public static bool CreateDataSource(IntPtr hwndParent,
int fRequest,
string lpszDriver,
string lpszAttributes)
{
return SQLConfigDataSource(hwndParent,
fRequest,
lpszDriver,
lpszAttributes);
}
}

Note:
The int 4 refers to the value "ODBC_ADD_SYS_DSN". To remove a system DSN use
the int 6. See below:

1 - ODBC_ADD_DSN (use this to add a user DSN)
2 - ODBC_CONFIG_DSN (use this to configure a user DSN)
3 - ODBC_REMOVE_DSN (use this to remove a user DSN)
4 - ODBC_ADD_SYS_DSN (use this to add a system DSN)
5 - ODBC_CONFIG_SYS_DSN (use this to configure a system DSN)
6 - ODBC_REMOVE_SYS_DSN (use this to remove a system DSN)

Also, you must include the following using reference:
using System.Runtime.InteropServices;
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Steve Sweales" <st***********@t-mobile.co.uk> wrote in message
news:Of*************@TK2MSFTNGP12.phx.gbl...
I'm trying to find some code on how to create a DSN using C# and
SQLConfigDataSource.

Can anybody help me please, before I tear my hair out!!

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2

Thats the same post as I have found, but I can't seem to get it to wrk
for SQL Server.

It always returns a false.
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Tried it and works fine

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Stevie" <St****@devdex.com> wrote in message
news:O$****************@tk2msftngp13.phx.gbl...

Thats the same post as I have found, but I can't seem to get it to wrk
for SQL Server.

It always returns a false.
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4

I can get it to work as it is (for Access), but I can not get it to
create a DSN for SQL Server.

Any ideas?
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
4
by: I_AM_DON_AND_YOU? | last post by:
There is one more problem I am facing but didn't get the solution. In my Setup Program I am not been able to create 2 things (when the program is intalled on the client machine ) : (1) create...
10
by: Zack Sessions | last post by:
Has anyone tried to create a SQL7 view using the CREATE VIEW command and ADO.NET? If so, is there a trick in trapping a SQL error when trying to create the view? I have a VB.NET app that, amoung...
9
by: Peter | last post by:
Hello£¬everyone, My program will collect a testing machine's data ,save the data and deal with the data everyday. I want to use vb.net to create database, add and delete tables or modify the...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
5
by: Michael | last post by:
Hello, I've created an ASP web page where users in our organization can create Active Directory computer accounts. The web page is running on a Server 2003 SP1 IIS 6 installation. The...
8
by: barb | last post by:
So that the world at large benefits from our efforts, here is one fully documented way to use Windows Irfanview freeware to create thumbnail web galleries (http://www.irfanview.com). STEP 1:...
2
by: masri999 | last post by:
I have a requirement in SQL 2005 in Development database 1. Schema dbo owns all objects (tables,views,SPs,UDFs etc) . 2. Only DBA's ( who are database owners ) can create, alter tables ....
4
by: JohnnyDeep | last post by:
I am trying to create a store proc that contain a create index with the cluster option and I receive DB21034E The command was processed as an SQL statement because it was not a valid Command...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.