Home All Groups Group Topic Archive Search About

Update ODBC DSN Properties in VB.NET Code?

Author
18 May 2006 3:21 PM
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?

Author
18 May 2006 8:50 PM
Carl M.
Try using an API call to SQLConfigDataSource; although this solution is not
specific to VB.Net, it will solve your problem.

<za***@construction-imaging.com> wrote in message
Show quoteHide quote
news:1147965666.077965.217700@j55g2000cwa.googlegroups.com...
>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?
>
Author
19 May 2006 4:51 AM
arthurjr07
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
Author
19 May 2006 4:18 PM
zacks
I should have known that this info was simply stored in the registry.
Thanks for the pointer and the sample code.