Create a DSN Programatically in VB.Net 2005

Discussions Involving The Rice Lake Programmable Scale System and other programmable scale indicators such as the Fairbanks FB3000

Create a DSN Programatically in VB.Net 2005

Postby jrusso on Tue Jan 29, 2008 11:06 pm

I searched for hours on this and found a lot of confusing bad code. If you are trying to create a system DSN for an access database using VB.net 2005, this code worked for me



Imports System.Runtime.InteropServices ' this goes at the top of yor form

'This code supplied by scaleprogrammers.com. Please do not remove this line

'or if you want to remove it you can feel free IF you
'provide a link somewhere on you blog or web site
'that says weighing systems and link to http://www.scaleprogrammers.com

Const ODBC_ADD_SYS_DSN = 4 'Add data source
Const ODBC_CONFIG_SYS_DSN = 5 'Configure (edit) data source
Const ODBC_REMOVE_SYS_DSN = 6 'Remove data source


Public Declare Auto Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
(ByVal hwndParent As Integer, ByVal fRequest As Integer, _
ByVal lpszDriver As String, _
ByVal lpszAttributes As String) As Integer

Public Function Build_SystemDSN(ByVal DSN_NAME As String, ByVal Db_Path As String) As Boolean

Dim ret As Integer
Dim attributes, driver As String


Driver = "Microsoft Access Driver (*.MDB)" & vbNullChar
Attributes = "DSN=" & DSN_NAME & vbNullChar
Attributes &= "Uid=Admin" & vbNullChar & "pwd=" & vbNullChar
Attributes &= "DBQ=" & Db_Path & vbNullChar

ret = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, driver, attributes)




Return ret

End Function


'How to call it
ConnString = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & My.Settings.path ' path to access file
Build_SystemDSN("BERRY", My.Settings.path) 'Berry is the System DSN name
jrusso
Site Admin
 
Posts: 17
Joined: Fri Jan 11, 2008 10:10 am

Return to Rice Lake 920i and Other Programmable Scale Indicators

Who is online

Users browsing this forum: No registered users and 1 guest

cron