Jack Henry
Jack Henry

Reputation: 302

Visual c#, connect to SQL server using Sql Server Authentication instead of Windows Authentication

I am trying to connect to a database using the following Connection String that will allow me to connect to the database using Sql Server Authentication instead of Windows Authentication. This is because my user name and password for Windows Authentication is different from the Sql Server Authentication.

 String connStr =
        @"Data Source = mySeverName;
        Initial Catalog = PMSystem;    
        Integrated Security = SSPI;
        User ID = myID;
        Password = myPassword;";

Here is the rest of the code

 DataSet PMSytem = new DataSet();
 String sqlProject = @" Select * from Project";
 SqlDataAdapter daProject = new SqlDataAdapter(sqlProject, connStr);
 daProject.FillSchema(PMSytem, SchemaType.Source, "Project");
 daProject.Fill(PMSytem, "Project");

When I run the environment it comes up that the login has failed for my user account that is associated with the Windows Authentication and not Sql Server Authentication. I have looked it up online and I cannot seem to get a direct answer. The error is the connection String. Is there anything I am missing or do you guys have any suggestions on how to go about this? Thanks in advance!

EDITED: Note this is a very small draft project

Upvotes: 0

Views: 141

Answers (2)

BugFinder
BugFinder

Reputation: 17858

In your code you set Integrated Security to be used - so by default irrelevant of username/password its trying to use your windows account. If you turn it to false, and just supply "myuser","mypassword" it will use sql authentication.

String connStr =
        @"Data Source = mySeverName;
        Initial Catalog = PMSystem;    
        Integrated Security = false;
        User ID = myID;
        Password = myPassword;";

Upvotes: 1

Rahul
Rahul

Reputation: 77846

If it's windows authentication then don't pass credentials. Remove the below statements from connection string

    User ID = myID;
    Password = myPassword;";

It should be

String connStr =
        @"Data Source = mySeverName;
        Initial Catalog = PMSystem;    
        Integrated Security = true;"

Upvotes: 0

Related Questions