How to connect database by using username and password in .net c#
I'm new to .net c#, hope this question is not sound silly. How can I connect to the database in web.config by using the username and password??
Example:
Following is the connection strings that I used to connect to the database. How could I write/set so that I can set the username (username = test) and password (password 开发者_StackOverflow= abc123) for this connection strings and so that it will allows me to access to the database?
<connectionStrings>
<add name="aConnectionString" connectionString="Data Source=AAA-LLLL-SQL-00;Initial Catalog=Database_Name;Connect Timeout=1;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Try something like this.
<connectionStrings>
<add name="aConnectionString" connectionString="Data
Source=AAA-LLLL-SQL-00;Initial Catalog=Database_Name;User Id=myUsername;Password=myPassword"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Use this to pull your connection string from the web.config.
http://msdn.microsoft.com/en-us/library/ms178411.aspx
Then you will use the .net Sql Provider to create a connection.
using(SqlConnection con = new SqlConnection(connectionstring))
{
con.Open();
// Perform operations here
}
Are you trying from IIS? Then you need to grant your App Pools account access to SQL Server.
CREATE LOGIN [IIS APPPOOL\ASP.NET v4.0] FROM WINDOWS WITH DEFAULT_DATABASE=[AAA-LLLL-SQL-00], DEFAULT_LANGUAGE=[us_english]
GO
Reference for ADO and ADO.NET connection strings http://www.sqlstrings.com/
<connectionStrings>
<add name="aConnectionString" connectionString="Server={ip};Database= {databasename}; User Id={username}; Password={password}"/>
</connectionStrings>
This should work replace {...} with your values.
<connectionStrings>
<add name="aConnectionString" connectionString="Data Source=AAA-LLLL-SQL-00;Initial Catalog=Database_Name; User Id=your username; Password=pwd;" providerName="System.Data.SqlClient"/>
</connectionStrings>
精彩评论