G
Guest
I'm having trouble using VB.Net to create a system DSN.
I cannot use DSN-less connection, must use ODBC.
I have seen the "Creating odbc dsn with C#".
I have seen many VB examples for creating an dsn for access db, but they seem inconsistent: E.g: Do I use \0 or chr$(0) or chr(0) for the null between attributes? Do I need the null values in the string?
Here is what I have, and it just returns false (or 'zero').
'-----START------
Imports System.Runtime.InteropServices
Class ODBCDLL
Public Declare Auto Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
(ByVal hwndParent As Long, ByVal fRequest As Long, _
ByVal lpszDriver As String, ByVal lpszAttributes As String) _
As Integer
End Class
Module CreateDSN
Private Const ODBC_ADD_SYS_DSN = 4 ' Add data source
Public Sub Main()
Dim dbpath as String = "C:\Program Files\AFolder\dbName.mdb"
Dim ret As Integer, Driver as String, Attributes as String
Driver = "Microsoft Access Driver (*.MDB)" & Chr(0)
Attributes = "DSN=" & "Hello" & Chr(0)
Attributes = Attributes & "Uid=Admin" & Chr(0) & "pwd=" & Chr(0)
Attributes = Attributes & "DBQ=" & dbpath & Chr(0) & Chr(0)
ret = ODBCDLL.SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, Driver, Attributes)
'ret is equal to 1 on success and 0 if there is an error
If ret <> 1 Then
MsgBox("DSN Creation Failed")
End If
End Sub 'Main
End Module
'-------------END--------------
On testing, this always returns 0 (into 'ret'). (It does the same thing when
using \0 instead of Chr(0). I'm not sure how to get any error messages.
Eg: I read we're supposed to use SQLInstallerError but have no idea how to
do that).
I'm not sure what else to try.
So, Any help would be appreciated (especially for VB.NET). Thank you...
I cannot use DSN-less connection, must use ODBC.
I have seen the "Creating odbc dsn with C#".
I have seen many VB examples for creating an dsn for access db, but they seem inconsistent: E.g: Do I use \0 or chr$(0) or chr(0) for the null between attributes? Do I need the null values in the string?
Here is what I have, and it just returns false (or 'zero').
'-----START------
Imports System.Runtime.InteropServices
Class ODBCDLL
Public Declare Auto Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
(ByVal hwndParent As Long, ByVal fRequest As Long, _
ByVal lpszDriver As String, ByVal lpszAttributes As String) _
As Integer
End Class
Module CreateDSN
Private Const ODBC_ADD_SYS_DSN = 4 ' Add data source
Public Sub Main()
Dim dbpath as String = "C:\Program Files\AFolder\dbName.mdb"
Dim ret As Integer, Driver as String, Attributes as String
Driver = "Microsoft Access Driver (*.MDB)" & Chr(0)
Attributes = "DSN=" & "Hello" & Chr(0)
Attributes = Attributes & "Uid=Admin" & Chr(0) & "pwd=" & Chr(0)
Attributes = Attributes & "DBQ=" & dbpath & Chr(0) & Chr(0)
ret = ODBCDLL.SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, Driver, Attributes)
'ret is equal to 1 on success and 0 if there is an error
If ret <> 1 Then
MsgBox("DSN Creation Failed")
End If
End Sub 'Main
End Module
'-------------END--------------
On testing, this always returns 0 (into 'ret'). (It does the same thing when
using \0 instead of Chr(0). I'm not sure how to get any error messages.
Eg: I read we're supposed to use SQLInstallerError but have no idea how to
do that).
I'm not sure what else to try.
So, Any help would be appreciated (especially for VB.NET). Thank you...