简体   繁体   中英

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

在此处输入图片说明

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

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM