I need to use code to set up an old-fashioned DSN, rather than a connection string, due to working with a legacy application. I can set it up manually using the DSN setup wizard. If I do that, it appears in the list of DSNs and appears in the registry. If I use the following code, it returns TRUE indicating success, but the DSN does not appear in the list of DSNs nor in the registry. Here is the code:
Imports System.Runtime.InteropServices
Public Class frmMain
<DllImport("ODBCCP32.DLL")>
Shared Function SQLConfigDataSource( _
ByVal hwndParent As Integer, _
ByVal fRequest As Integer, _
ByVal lpszDriver As String, _
ByVal lpszAttributes As String) As Boolean
End Function
Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
Const ODBC_ADD_SYS_DSN = 4
Dim Driver As String = "SQL Server"
Dim Attributes As String =
"DSN=" & "WebCoolDSN" & Chr(0) &
"Database=" & "WebCool" & Chr(0) &
"Description=" & "WebCool DSN" & Chr(0) &
"Server=" & "semsdb02\dev1" & Chr(0) & Chr(0)
Dim Result As Boolean
Result = SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, Driver, Attributes)
MsgBox(Result.ToString())
End Sub
End Class
Here is some of my research:
http://msdn.microsoft.com/en-us/library/ms716476(VS.85).aspx
How do I create an ODBC DSN entry using C#?
See comments for more. The research got me to the point where:
- I found out about the
SQLConfigDataSource
API call. - I found out the format of the parameters that supposedly would work.
- I found out what to look for in the registry to see what gets put in when I create the DSN manually.
- I found out you can't put in a username and password with the DSN.
- I found out that it might be possible in InstallShield, but I would need to have my department purchase me a higher-level version of it.
- I found out another way to do this is to edit the registry directly, but that is not preferred because it could be dangerous.