Connecting to an MS SQL Express database

Hi Experts,

I am using the following code trying to connect to an MS SQL Express 2008 database. I can connect and query the database from within the SQL Server Management Studio so I think it is set up correctly. I have not set a password for the database so the credentials I am using are the windows admin credentials for the account that was used to set up the database.

The error I get when trying to connect is: Login failed for user 'admin'.

I have tried both the connection strings in the code with the same results.

Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class SQLControl

    Public SQLCon As New SqlConnection With {.ConnectionString = "Server=ACERES1\SQLEXPRESS;Database=TestDb;User=admin;Pwd=Admin-123;"}
    'Public SQLCon As New SqlConnection With {.ConnectionString = "Server=ACERES1\SQLEXPRESS;Database=TestDb;User Id=admin;Password=Admin-123;"}
    Public SQLCmd As SqlCommand

    Public Function HasConnection() As Boolean

        Try

            SQLCon.Open()
            SQLCon.Close()
            Return True

        Catch ex As Exception

            MsgBox(ex.Message)
            Return False

        End Try

    End Function

End Class

Open in new window

DColinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Windows credentials work integrated, thus you don't need to repeat them. Using credentials like you did, are SQL Authentication credentials.

Thus change you connection string to:

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
It is not Windows credentials that you need to pass. You need to create Logins directly in SSMS under Security.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.