Connecting to an MS SQL Express database

DColin
DColin used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
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

Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
It is not Windows credentials that you need to pass. You need to create Logins directly in SSMS under Security.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial