user4912134
user4912134

Reputation: 1043

ConnectionString in the web.config to connect to SQL Server

I have database locally in the server I am working on creating the Web API. With the windows authentication I am able get in the SQL server and access the database and table from SQL Server Management Studio. But with this connection string

<connectionStrings>
    <add name="DBConnection"
         providerName="System.Data.SqlClient"
         connectionString="Data Source=NameoftheServer;Initial Catalog=NameofDatabase;Integrated Security=False;User Id=UserIDusedin nManagementStudio;Password=Password;MultipleActiveResultSets=True" />
</connectionStrings>

and the Web API I am giving it as

  string strcon = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
   SqlConnection DbConnection = new SqlConnection(strcon);
   DbConnection.Open();

But when I try to call the API it throws exception in

enter image description here

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Login failed for user

enter image description here

Upvotes: 0

Views: 11742

Answers (2)

H.Mikhaeljan
H.Mikhaeljan

Reputation: 813

This is what i use myself and its working.

Add

name = "DBConnection"
providerName = "System.Data.SqlClient"
connectionString = "Data Source=Source;Initial Catalog=DatabaseName;User ID=User;Password=Password;"

conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;

If its not working the problem is maybe in the connection string itself since it can be a little different depending on database.

Upvotes: 0

Backs
Backs

Reputation: 24913

Check authentication mode of your SQL-Server, edit it if needed.

Maybe, you need to use Integrated Security in your conneciton string. So, you needn't to set up user id and passowrd in connection string, but need to run your application with credentials of trusted user.

Upvotes: 2

Related Questions