How to get connection strings to various databases in ASP.NET 2.0


How to use connection strings to various Databases in ASP.NET 2.0

Some of the values and strings are used across all the pages of a website. It is a tedious process to code those values and strings in each page of the website.

The connection string will be used in many pages of the web site if you are developing a database driven website. Let us say that you have hard coded the connection string in all the pages, around 30 pages. Now if the value of the connection string changes, you have a headache of changing all the values of the connection string in all the 30 pages. This can’t be done easily and if you are to change the string again after about one month, it is going to be a tedious task.

Hence in such scenarios it is a practice to store those values in a central repository from where you can retrieve it and use it in all the pages. For example if you store the connection string in a central place like a web.config file you can retrieve those connection strings from the web.config file and use it. Previous to ASP.Net 2.0 the connection strings were stored in web.config file as given below:

<configuration>
<appSettings>
<add key=”connectionString” value=”connection_string_value_here” />
</appSettings>

<system.web>

</system.web>
</configuration>

The connection string is stored as key and value pair in the <appSettings> element of the web.config file. This value can be retrieved by writing code like:

ConfigurationSettings.AppSettings(“connectionString”)

Instead of using the <appSettings> section you can also add your own sections in the web.config file to have all your string value that you will be using in your application.

ASP.Net 2.0 provides with a special class for retrieving those customized string values.

This class is called the ConfigurationManager class.

Using this class you will be retrieving values by writing the following code for the above config file.

ConfigurationManager.AppSettings[“connectionString”]

To open the ConnectionStrings section of the web.config file, you can use code like

ConfigurationManager.ConnectionStrings[“connectionString”].ConnectionString

The above code is used to retrieve ConnectionStrings from the web.config file as given below:

<connectionStrings>
<add name=”connectionString” connectionString=”Data Source=.;Initial Catalog=db_products;Integrated Security=True” providerName=”System.Data.SqlClient” />
</connectionStrings>

This code comes under the <configuration> section of the web.config file.

Here you can use the connectionStrings tag to store multiple Connection string to multiple databases and use it at your might.

Like:

<connectionStrings>
<add name=”connectionString” connectionString=”Connect_String_Value” providerName=”Connection_Provider” />

<add name=”myCString” connectionString=”Connect_String_Value” providerName=”Connection_Provider” />

<add name=”newCString” connectionString=”Connect_String_Value” providerName=”Connection_Provider” />
</connectionStrings>

You can use it in you code like :

1. ConfigurationManager.ConnectionStrings[“connectionString”].ConnectionString

2. ConfigurationManager.ConnectionStrings[“myCString”].ConnectionString

3. ConfigurationManager.ConnectionStrings[“newCString”].ConnectionString


Happy Connecting to database 🙂

Connecting MySQL From PHP


Hi,
Although many of you know how to connect MySQL Server which is Running on your machine (localhost) or to any other server from a PHP Code, but I think I can write a few lines for those who does not know how to do it.

If you are very much new to PHP and wondering about how to install php in your Local machine rather than using a server which costs very much and for the beginners it is not worthful, then you may visit http://beg-webdev.blogspot.com/ which can guide you through the installation process. I hope it will help you to install Apache (The web server), MySQL (the Database Server) and PHP (Hypertext Preprocessor) the coding language.
Php is very easy to learn. You can buy a small book from WROX (Beginning PHP,MySQL, Apache Web Development or PHP5 or PHP4) and from there you can learn the basic syntax of PHP (which is very much similar to C).

Now I assume that you know a little bit of php and you have installed Apache, Php and Mysql successfully.
[You need to uncomment the line  ;extension=php_mysql.dll by removing the beginnign ; and restart Apache Web server. If it still throwing errors such as php_mysql.dll not found, then you may need to restart the computer also.]
You can find that you MySQL is running perfectly and accessible from PHP from a piece of code

<?php
//This a function which shows php information
phpinfo();
?>

Save this file as you wish in the Apache s htdocs directory (which is the directory from where you php scripts will run, you can change this also by configuring httpd.conf file in Apcahes conf directory). I prefer to use the name of the file phpinfo.php. Hit this file in the internet explorer like (http://localhost/phpinfo.php) and you will see the informations about installed php on your server. And you can find the MySQL support block from there. If this block is present then you are ready to connect MySQL from php.

So we can start:
1. create a file name conf.php (you can choose other names also)
  put below contents in the file,

 <?php
   $host = “localhost”; //Host where this database is running.
   $user_name = “user”; //Your MySQL Server’s user name
   $password = “pass”; //Your MySQL Server’s  Password
   $db_name = “test”; //Your MySQL Server’s Database.

  //Now connect
  $link = mysql_connect($host,  $user_name, $password);
  if($link)
  {
        echo “Connection to <b>$host</b> successful”;
       //Now you are connected to MySQL, so choose the databse you want to use
       $is_selected = mysql_select_db($db_name)  ;
       //Check the selection
      if($is_selected)
      {
          echo “Database: $db_name selected successfully”;
       }else{
         echo “Error selecting database $db_name;
       }
  }else{
    echo “Error connecting MySQL Server at $host”;
  }

 ?>

Save this script to your servers document root folder (htdocs) and call this script from Internet Explorer or Mozilla.
If you php-mysql configuration is ok then this script will run without any error.

Also for mysql errors you can change this script to 

<?php
   $host = “localhost”; //Host where this database is running.
   $user_name = “user”; //Your MySQL Server’s user name
   $password = “pass”; //Your MySQL Server’s  Password
   $db_name = “test”; //Your MySQL Server’s Database.

  //Now connect
   mysql_connect($host,  $user_name, $password) or die(mysql_error());
  
  //Now you are connected to MySQL, so choose the databse you want to use
   mysql_select_db($db_name)  or die(mysql_error());
    echo “Database: $db_name selected successfully”;
  ?>

 In this case if any errors occures in the runtime, it will automatically notify you with the error details thrown by mysql server.