Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I tried My best to add ODBC System Data Source for Access (.accdb) Database using VB.NET. Searching from Google I tried many Functions but nothing worked.

Code I tried is :

VB
Sub createDSN()
        Const ODBC_ADD_SYS_DSN = 4 ' Add data source
        Dim dbpath As String = "C:\Jenit\Data\001.accdb"

        Dim ret As Integer, Driver As String, Attributes As String

        Driver = "Microsoft Access Driver (*.MDB,*.accdb)" & Chr(0)
        Attributes = "DSN=" & "Hello" & Chr(0)
        Attributes = Attributes & "Uid=Admin" & Chr(0) & "pwd=pwd" & Chr(0)
        Attributes = Attributes & "DBQ=" & dbpath & Chr(0) & Chr(0)

        ret = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, Driver, Attributes) 'Error Here


        'ret is equal to 1 on success and 0 if there is an error
        If ret <> 1 Then
            MsgBox("DSN Creation Failed")
        Else
            MsgBox("Successful")
        End If

End Sub 'Main


Error is :

A call to PInvoke function 'j!j.Form1::SQLConfigDataSource' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Please Help.
Posted
Comments
Dave Kreskowiak 5-Sep-13 9:14am    
A question first...Why are you even using a DSN?? All that is is a connection string in a text file. You get the same functionality by putting your connection string in your applications app.config file.
jenitshah 5-Sep-13 9:20am    
How ? Can you please elloborate?
Bala Selvanayagam 5-Sep-13 14:24pm    
Jenitshah,

The method to access the database using "DSN" is quiet old method and not been used any more in the industry.

putting the connection string in the app.config file and using it to connect to the database is a commonly used method. please see the similar post http://www.codeproject.com/Questions/254402/How-To-Store-and-retrive-ConnectionString-from-app if you need further info


if you still have issue let me know
jenitshah 6-Sep-13 10:03am    
OK. I decided to switch to another way. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900