Update ODBC DSN Properties in VB.NET Code?

  • Thread starter Thread starter zacks
  • Start date Start date
Z

zacks

I have a DSN that I frequently have to change the default database. Is
there a way to do this in VB.NET code instead of having to go through
the setup wizard every time?
 
Try using an API call to SQLConfigDataSource; although this solution is not
specific to VB.Net, it will solve your problem.
 
Private Function CreateDSN(ByVal DB_Name As String, _
ByVal DSN As String, _
ByVal Description As String, _
ByVal Driver_Name As String, _
ByVal userid As String, _
ByVal password As String, _
ByVal Server_Name As String, _
ByVal port As String, _
ByVal stroption As String, _
ByVal stmt As String _
) As Boolean

Dim lResult As Long
Dim hKeyHandle As Long
Dim msg1 As String

Dim regHandle As RegistryKey ' Stores the Handle to Registry in
which values need to be set

Dim reg As RegistryKey = Registry.LocalMachine
Dim conRegKey1 As String = "SOFTWARE\ODBC\ODBC.INI\" & DSN
Dim conRegKey2 As String = "SOFTWARE\ODBC\ODBC.INI\ODBC Data
Sources"

Try
regHandle = reg.CreateSubKey(conRegKey1)
regHandle.SetValue("Database", DB_Name)
regHandle.SetValue("Description", Description)
regHandle.SetValue("Driver", Driver_Name)
regHandle.SetValue("Option", stroption)
regHandle.SetValue("Password", password)
regHandle.SetValue("Port", port)
regHandle.SetValue("Server", Server_Name)
regHandle.SetValue("Stmt", stmt)
regHandle.SetValue("User", userid)
regHandle.SetValue("LastUser", userid)
regHandle.Close()
reg.Close()

regHandle = reg.CreateSubKey(conRegKey2)
regHandle.SetValue(DSN, "SQL SERVER")
regHandle.Close()
reg.Close()
Catch err As Exception

End Try
End Function


if there is an existing DSN, it will overwite it.

HTH
 
I should have known that this info was simply stored in the registry.
Thanks for the pointer and the sample code.
 
Back
Top