We help IT Professionals succeed at work.

How to create DSN at run-time?

indiagenious
indiagenious asked
on
hi,

i wanted to create DSN at run-time. But, not getting how to create that.

please, help me.

it would be easy for me if you can provide with the code.

thankx in advance.

indiagenious.....
Comment
Watch Question

Try this link to create a System DSN

http://www.freevbcode.com/ShowCode.Asp?ID=502

Regards
Andrew

Commented:
Private Const REG_SZ = 1
Private Const HKEY_LOCAL_MACHINE = &H80000002

Private Declare Function RegCreateKey Lib "advapi32.dll" Alias _
   "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
   phkResult As Long) As Long

Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias _
   "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
   ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal _
   cbData As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" _
  (ByVal hKey As Long) As Long



Public Function AjouterDSN(Server As String, DatabaseName As String, UserName As String)
    Dim Counter As Integer
    Dim DriverName As String
    Dim DriverPath As String
    Dim DataSourceName As String
    Dim lResult As Long
    Dim hKeyHandle As Long
   
    'Specification des parametres DSN
    DriverName = "SQL Server"
    DriverPath = "sqlsrv32.dll"  'dll du SQL Server
    DataSourceName = ""          'Specification du DSN ici
       
    'Create DSN Key
    lResult = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\" & DataSourceName, hKeyHandle)
       
    'Set les valeurs du DSN Key
    lResult = RegSetValueEx(hKeyHandle, "Server", 0&, REG_SZ, ByVal Server, Len(Server))
    lResult = RegSetValueEx(hKeyHandle, "Driver", 0&, REG_SZ, ByVal DriverPath, Len(DriverPath))
    lResult = RegSetValueEx(hKeyHandle, "Database", 0&, REG_SZ, ByVal DatabaseName, Len(DatabaseName))
    'lResult = RegSetValueEx(hKeyHandle, "UserName", 0&, REG_SZ, ByVal UserName, Len(UserName))
   
    'Close DSN Key
    lResult = RegCloseKey(hKeyHandle)
    lResult = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", hKeyHandle)
    lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&, REG_SZ, ByVal DriverName, Len(DriverName))
    lResult = RegCloseKey(hKeyHandle)
End Function
Éric MoreauSenior .Net Consultant
BRONZE EXPERT
Top Expert 2016

Commented:
You will find all the source code for that at http://support.microsoft.com/support/kb/articles/q184/6/08.asp

Commented:
The indiagenious account has been closed for failure to provide feedback even after several requests.

Other participants, please advise as to who should get points for this.

modder
Community Support
Éric MoreauSenior .Net Consultant
BRONZE EXPERT
Top Expert 2016

Commented:
AndrewDev was the first proposing a correct solution.

Commented:
ok
Thanks for the points, but isn't it a bit harsh closing an account after the question has been open for only four days or are there other outstanding questions he hasn't responded to?
Regards
Andrew

Commented:
Lots, Andrew.... lots.
In that case, thanks again for the points and thanks for keeping on top of things for us.
Your doing a grand job :-)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.