sd_dracula
sd_dracula

Reputation: 3896

C# WinForms not accepting connection string user details

I have a connection string in App.config like so:

<add name="connectionString"
      connectionString="Data Source=SERVER;Initial Catalog=DB;Integrated Security=True;User ID=domain\username;Password=12345;Connection Timeout=300"
      providerName="System.Data.SqlClient" />

I then call the string in code behind like so:

string conSTR = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
                SqlDataReader reader;
                using (SqlConnection sqlConn = new SqlConnection(conSTR))
                using (SqlCommand cmd = new SqlCommand(SQLQuery, sqlConn))
                {
                    sqlConn.Open();
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                     .... stuff happens here
                    }
                }

MY local account does not have access to the server but the service account passed into the connection string does.

The error I get is:

Cannot open database "DB" requested by the login. The login failed.

Login failed for user 'domain\MyUserName'.

For some reason it is completely ignoring the user name/password in the connection string and tries to connect using my account.

How can I fix that?

Upvotes: 0

Views: 593

Answers (2)

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25475

That because you're specifying Integrated Security=True;. This overrules any other authentication settings, remove it and it will use the username and password supplied.

Upvotes: 0

Jay
Jay

Reputation: 2129

you have integrated security = true in your connection string, remove that.

Upvotes: 4

Related Questions