Search This Blog

Showing posts with label Database Connection. Show all posts
Showing posts with label Database Connection. Show all posts

Wednesday, 28 September 2011

SQL Database Connection through App Settings

Hi guys here is a method to set database connectivity through app settings in the web.config file


SqlConnection .NET strings

Standard Security:
1. "Data Source=Your_Server_Name;Initial Catalog= Your_Database_Name;UserId=Your_Username;Password=Your_Password;"
2. "Server=Your_Server_Name;Database=Your_Database_Name;UserID=Your_Username;Password=Your_Password;Trusted_Connection=False"

Trusted connection:
1. "Data Source=Your_Server_Name;Initial Catalog=Your_Database_Name;Integrated Security=SSPI;"
2."Server=Your_Server_Name;Database=Your_Database_Name;Trusted_Connection=True;"  


The above are different connection strings which we apply accordingly.

Let me take a general approach with standard security, First we need to open web.config file and below <configuration> type

<appSettings>
<add key ="ConnectionStr" value="server = server name; database = 'database name'; uid = 'username'; pwd = 'password' />
</appSettings>

The appsettings in your webconfigwill appear as in the figure below:



Once the appsettings are given let us go to our C# or apsx.cs page where we need to set the connectivity as shown below

string connectionstr = ConfigurationManager.AppSettings["ConnectionStr"];

This is set as a global string, Once this is done this string is added to our SqlConnection;

SqlConnection con = new SqlConnection(connectionstr);

Now our connection is set :)

Observe the example figure below: