473,545 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create / enum ODBC source

My app uses an ODBC connection for a .mdb file. I want the app to create the
source if it does not already exist (I copy a blank .mdb file to the users
data store if it doesn't exist).

I see how to do this using SQLConfigDataSo urce() and that works ok. Thing
is, how to handle if the source already exists.
Is there a way to check & see if the source already exists?

Alternately, if it does exist & I call SQLConfigDataSo urce() does it mess up
/ break anything? If not, then no need to bother to enum the
sources....thou gh this hardly seems like a good practice.

Also, I have a dumb question - when I choose the DSN to use if VS2005 Data
Sources, is there a way to tell if it is from System or User ?

Thank you,

--
Dale Sampson
http://www.dalesplace.net

I tried using SQLDataSources Lib "odbc32.dll - but haven't been able to get
that to work.
I use this code :
http://vbnet.mvps.org/index.html?cod...atasources.htm.
I get a weird return code (1949695999) from SQLAllocHandle & the returned
handle is zero.
calls to SQLDataSources get return value = 65534.
These do not seem to be Win32 or ODBC error codes.

The code works fine in VB6 so I assume I'm messing up something in the
Declares?

Here's what I used:

'Constant Declaration
Private Const ODBC_ADD_DSN = 1 ' Add data source
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const vbAPINull As Long = 0 ' NULL Pointer
Const ODBC_ADD_SYS_DS N = 4 'Add data source
Const ODBC_CONFIG_SYS _DSN = 5 'Configure (edit) data source
Const ODBC_REMOVE_SYS _DSN = 6 'Remove data source
Private Const SQL_MAX_DSN_LEN GTH As Long = 32
Private Const SQL_MAX_DESC_LE NGTH As Long = 128
Private Const SQL_SUCCESS As Long = 0
Private Const SQL_FETCH_NEXT As Long = 1
Private Const SQL_NULL_HANDLE As Long = 0
Private Const SQL_HANDLE_ENV As Long = 1
Private Const SQL_ATTR_ODBC_V ERSION As Long = 200
Private Const SQL_OV_ODBC3 As Long = 3
Private Const SQL_IS_INTEGER As Long = (-6)
Const SQL_ERROR = (-1)
Const SQL_SUCCESS_WIT H_INFO = 1

Private Declare Function SQLDataSources Lib "odbc32.dll " _
(ByVal hEnv As Int32, _
ByVal fDirection As Int16, _
ByVal szDSN As String, _
ByVal cbDSNMax As Int16, _
ByVal pcbDSN As Int16, _
ByVal szDescription As String, _
ByVal cbDescriptionMa x As Int16, _
ByVal pcbDescription As Int16) As Int32

Private Declare Function SQLAllocHandle Lib "odbc32.dll " _
(ByVal HandleType As Int16, _
ByVal InputHandle As Int32, _
ByVal OutputHandlePtr As Int32) As Int32

Private Declare Function SQLSetEnvAttr Lib "odbc32.dll " _
(ByVal EnvironmentHand le As Int32, _
ByVal dwAttribute As Int32, _
ByVal ValuePtr As Int32, _
ByVal StringLen As Int32) As Int32

Private Declare Function SQLFreeHandle Lib "odbc32.dll " _
(ByVal HandleType As Int16, _
ByVal Handle As Int32) As Int32

Private Sub GetUserSystemDS N()

Dim hEnv As Long 'handle to the environment
Dim sServer As String
Dim sDriver As String
Dim nSvrLen As Integer
Dim nDvrLen As Integer
Dim Ret As Long = 0
Dim ret2 As Long = 0

'obtain a handle to the environment
ret2 = SQLAllocHandle( SQL_HANDLE_ENV, _
SQL_NULL_HANDLE , hEnv)
'
' ret2 = 1949695999; hEnv=0
'
If ret2 <0 Then

'if successful, set the
'environment for subsequent calls
If SQLSetEnvAttr(h Env, _
SQL_ATTR_ODBC_V ERSION, _
SQL_OV_ODBC3, _
SQL_IS_INTEGER) <0 Then

'
' return val = 1949695998; hEnv = 0
'
'
'set up the strings for the call
sServer = Space$(SQL_MAX_ DSN_LENGTH)
sDriver = Space$(SQL_MAX_ DESC_LENGTH)

'load the DSN names
Do While Ret = SQL_SUCCESS
Ret = SQLDataSources( hEnv, _
SQL_FETCH_NEXT, _
sServer, _
SQL_MAX_DSN_LEN GTH, _
nSvrLen, _
sDriver, _
SQL_MAX_DESC_LE NGTH, _
nDvrLen)
'
' ret = 65534
'
'
'add data to the controls
Debug.Print(sSe rver.Substring( 1, nSvrLen))
'List1.AddItem( Left$(sServer, nSvrLen))
'List2.AddItem( Left$(sDriver, nDvrLen))

'repad the strings
sServer = Space$(SQL_MAX_ DSN_LENGTH)
sDriver = Space$(SQL_MAX_ DESC_LENGTH)

Loop

End If 'If SQLSetEnvAttr

'clean up
Call SQLFreeHandle(S QL_HANDLE_ENV, hEnv)

End If 'If SQLAllocHandle

'since each DSN returned its corresponding
'driver, and a given driver can be used
'for multiple DSN's, remove any adjacent
'duplicates

End Sub
Nov 10 '06 #1
2 2741
Hi Dale,

To enumerate ODBC DSN, you need to look at following registry keys:

User DSNs:
HKEY_CURRENT_US ER\SOFTWARE\ODB C\ODBC.INI\ODBC Data Sources -contains
the DSN name and driver that it uses
HKEY_CURRENT_US ER\SOFTWARE\ODB C\ODBC.INI\<dsn name-contains the DSN
configuration details

System DSNs:
HKEY_LOCAL_MACH INE\SOFTWARE\OD BC\ODBC.INI\ODB C Data Sources -contains
the DSN name and driver that it uses
HKEY_LOCAL_MACH INE\SOFTWARE\OD BC\ODBC.INI\<ds n name-contains the DSN
configuration details
Calling SQLConfigDataSo urce with ODBC_ADD_DSN for existing DSN will change
its configuration. If you always want to use new settings for the DSN, then
I think you don't need to check for the DSN's existence first.

As for the VS2005 Data Sources, I'm afraid there're no easy way to tell if
the DSN is System or User. If you think this should be implemented, please
feel free to submit your feedback at
http://connect.microsoft.com/Main/co...ContentID=2220 which
is monitored by our product team directly. Thank you for your understanding.

By the way, you want to be aware following known issue about
SQLConfigDataSo urce:

#BUG: SQLConfigDataSo urce Returns TRUE Even Without Registry Permissions
http://support.microsoft.com/kb/294177/

This issue seems still exist on Windows XP SP2, we're sorry for the
inconvience.

Sincerely,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 13 '06 #2
Thank you Walter. That works ok. Good point with regard to simply writing
the DNS.

Regards,

--
Dale Sampson
http://www.dalesplace.net

"Walter Wang [MSFT]" <wa****@online. microsoft.comwr ote in message
news:1O******** ******@TK2MSFTN GXA01.phx.gbl.. .
Hi Dale,

To enumerate ODBC DSN, you need to look at following registry keys:

User DSNs:
HKEY_CURRENT_US ER\SOFTWARE\ODB C\ODBC.INI\ODBC Data Sources -contains
the DSN name and driver that it uses
HKEY_CURRENT_US ER\SOFTWARE\ODB C\ODBC.INI\<dsn name-contains the DSN
configuration details

System DSNs:
HKEY_LOCAL_MACH INE\SOFTWARE\OD BC\ODBC.INI\ODB C Data Sources -contains
the DSN name and driver that it uses
HKEY_LOCAL_MACH INE\SOFTWARE\OD BC\ODBC.INI\<ds n name-contains the DSN
configuration details
Calling SQLConfigDataSo urce with ODBC_ADD_DSN for existing DSN will change
its configuration. If you always want to use new settings for the DSN,
then
I think you don't need to check for the DSN's existence first.

As for the VS2005 Data Sources, I'm afraid there're no easy way to tell if
the DSN is System or User. If you think this should be implemented, please
feel free to submit your feedback at
http://connect.microsoft.com/Main/co...ContentID=2220
which
is monitored by our product team directly. Thank you for your
understanding.

By the way, you want to be aware following known issue about
SQLConfigDataSo urce:

#BUG: SQLConfigDataSo urce Returns TRUE Even Without Registry Permissions
http://support.microsoft.com/kb/294177/

This issue seems still exist on Windows XP SP2, we're sorry for the
inconvience.

Sincerely,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your
reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no
rights.


Nov 14 '06 #3

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

Similar topics

9
3540
by: Albretch | last post by:
.. I am trying to create a database in a MS Access DB via JDBC drivers. I have tried both sun.jdbc.odbc.JdbcOdbcDriver and ids.sql.IDSDriver From some reason both drivers Exceptions tell me 'Syntax error in CREATE TABLE statement' even though I am not creating a table, but a Database // - - - - - - - - - - - - sun.jdbc.odbc.JdbcOdbc...
7
8830
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 want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and...
8
2833
by: DerekS | last post by:
Hi, I've been pulling my hair out trying to write a simple method to programatically create a system DSN with all parameters on a remote machine. I have ensured I have the correct permissions on that remote machine. I have also review many MSDN and codeproject.com articles. Any help would be great. Thanks, Derek
5
15217
by: Steven Blair | last post by:
Hi, As the title suggests, I am looking for soemthing which would essentially give me a nested enum, or perhaps someone can suggest a more suitable approach to my problem. I have a DLL which allows the user to select which type of DB they are using and also how to connect (ODBC, OleDb etc) and I want this all available through one type:
0
7425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7682
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5351
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.