Cannot connect to SQL Server Express using a connection string

  • hello,

    I am very new to VB.Net and SQL Server. I have SQL Server 2005 (Express) and Visual Studio Express 2008 Editions on my computer. I am trying to access pubs database from my vb.net program but sometimes I get an error saying "Login failed for user 'sa'. The user is not associated with a trusted SQL Server Connection".

    It says "Login failed for user. The user is not associated with a trusted SQL Server Connection" if I don't give any value for user in the connection field."

    Here is my code:

    Imports System.Data.SqlClient

    Public Class Form1

    Dim sqlMyConnection As SqlConnection

    Dim sqlMyCommand As SqlCommand

    Dim sqlDR As SqlDataReader

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    sqlMyConnection =

    New SqlConnection ("server=Home\SQLExpress;uid=sa;pwd=;database=pubs")

    Try

    sqlMyConnection.Open()

    MsgBox("Connected!")

    sqlMyCommand = New SqlCommand("Select * from authors", sqlMyConnection)

    sqlDR = sqlMyCommand.ExecuteReader

    While sqlDR.Read

    MsgBox(sqlDR(0).ToString)

    MsgBox(sqlDR(1).ToString)

    MsgBox(sqlDR(2).ToString)

    MsgBox(sqlDR(3).ToString)

    End While

    sqlDR.Close()

    sqlMyConnection.Close()

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    End Sub

    End Class

    Please let me know as to how I can get my code work.

    Thanks.

  • it has to do with mix mode being allowed or not. on express, it's turned off by default, but usually on for production servers.

    1) Open up SQL Server Management Studio (or Enterprise Manager in SQL 2000) and login into the database using Windows Authentication.

    2) On the right side, right click1 on the database, then go down and click2 on Properties. In SQL 2000, you may need to expand the tree view to expose the database that you want.

    3) Click3 in Security.

    4) Under Authentication, click4 on ‘SQL Server and Windows Authentication mode‘ in Management Studio, or ‘SQL Server and Windows‘ in Enterprise Manager, and then click5 OK.

    with pictures for people like me who like to see the screens:

    http://www.carlj.ca/2008/01/09/the-user-is-not-associated-with-a-trusted-sql-server-connection/

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks Lowell.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply