Link to home
Start Free TrialLog in
Avatar of lbsi
lbsiFlag for United States of America

asked on

Using a DSN connection in a VB.net program

Good Afternoon

I use the following code to build my connection string in VB.net:

  Dim BuildConnectionString As String
  BuildConnectionString = "DSN=" + g_DSNName + ";UID=" + g_UserId + ";PWD=" + g_Password
  Dim odbcConnect As New OdbcConnection(BuildConnectionString)


Being that my DSN is set-up with sa, I am not really sure I fully understand how the connection string actually works.
Is the UID and PWD, used in the connection string, the ones in the DSN or is the UID and PWD for connecting to the database once the DSN gets
you to the server?

Thanks,
Ed

Avatar of Dimandja
Dimandja
Flag of United States of America image

When using a DSN, you shoyuld not specify anything else.

Example using DSN:

        Dim sConnString = "DSN=Pubs_DSN"
        Dim oODBCConnection = New Odbc.OdbcConnection(sConnString)
         Try
            oODBCConnection.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


Example DSN-less:

        Dim connectionString = "Driver={SQL Server};Server=SQLservername;Database=pubs;Uid=;Pwd=;"
        Dim oODBCConnection = New Odbc.OdbcConnection(connectionString)
        Try
            oODBCConnection.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try



Pubs_DSN is:

Data Source Name: Pubs_DSN
Server: <SQLservername>
Database: pubs
Use Integrated Security: Yes
Avatar of riyazthad
riyazthad

Ed,

Are you getting any error from your code? Are you using windows authetication, DB server credentials? If windows authentication are using , just need DSN in connection string.

If you are getting error from above code add 'Trusted_Connection=False' in your connection string.

Thad
Go here for more examples: http://www.connectionstrings.com/
ASKER CERTIFIED SOLUTION
Avatar of Dimandja
Dimandja
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lbsi

ASKER

I apologize for not accepting the answer from Dimandja.  The information assisted me in finding the solution.  Please assign point total to Dimandja.  In the future, I will be more diligent in accepting answers and assigning points.

Thanks,
Ed