2

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 = 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>
2
  • Out of curiosity, why do you want to do this? Windows authentication is far more secure -- since not having to put passwords in easily readable config files is a Good Thing. Commented Sep 14, 2011 at 23:16
  • 2
    see connectionstrings.com/sql-server-2008 - this should be obvious Commented Sep 14, 2011 at 23:19

6 Answers 6

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

More general: Check connectionstrings.com for most used database connection string variants. Beats most documentation.
1

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

}

Comments

1

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

1 Comment

While this is one of the ways Microsoft says you can do it I myself have always believed this to be a hack. I really think they missed the boat on using Windows Auth with IIS and SQL Server.
0

Reference for ADO and ADO.NET connection strings http://www.sqlstrings.com/

Comments

0
    <connectionStrings>
       <add name="aConnectionString" connectionString="Server={ip};Database= {databasename}; User Id={username}; Password={password}"/>
</connectionStrings>

This should work replace {...} with your values.

Comments

0
<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.