Nixser.com


Nixser.com

Debian, Internet, Blog, Tech Tips, and my craps.

PHP VS ASP.NET - Connecting to Database.

Continue the series of PHP VS ASP.NET. My previous post covered the installations of both MySQL and MSSQL database on Windows XP Pro. Today we are going to connect to the database programmatically, as in using the database along with your server-side scripts.

PHP
PHP + MySQL is always my favorite combo for web application. The connection to MySQL database in PHP is fairly simple.

Codes:

<?php
$conString 
mysql_connect(“host”“username”“password”);
mysql_select_db(“databasename”$conString);
?>



The “host” can be a IP address, or a hostname. In our case, “localhost” or “127.0.0.1″ would be the correct value. “username” would be the database username, and so for “password”. You should have created a database for your own testing purpose, use that name for “databasename”.

It is recommended that you store the database connection string above in a separate file so you can reuse it in different pages. Some believe that storing this file out of your web root folder is a good security measure. For now we are going to save it in the same folder, as file name conn.php

Codes:

<?php
//include the connection string file.
include_once(‘conn.php’);

//sql query
$sql “select * from table”;
//execute query
$result mysql_query($sql) or die(“Query Error - ” mysql_error());

//check if any record returned
if(mysql_num_rows($result) != 0)
{
     
//loop through the records
     
while($record mysql_fetch_assoc($result))
     {
            
//do something
     
}
}
?>


ASP.NET
Now the ASP.NET way. In every ASP.NET application directory, lies a Web.config file. If you are not using any Microsoft dev tool, just a notepad, you will have to create this file manually. The connection string can be stored in Web.config file for the reusability, and better security.

Here’s a sample

Codes:

<?xml version=“1.0″ encoding=“utf-8″?>
<configuration>
   <system.web>
      <connectionStrings>
      <add 
          name=”connString” 
          connectionString=”
               Data Source=serverName;
               Initial Catalog=Northwind;
               Persist Security Info=True;
               User ID=userName;
               Password=password” 
          providerName=”System.Data.SqlClient”
      />
     </connectionStrings>
   </system.web>
</configuration>

“Data Source” would be your server hostname, “Initial Catalog” would be the database name.
So in your aspx pages, this is how you deal with the database connection string.

Codes:

SqlConnection conn = new SqlConnection
(System.Configuration.ConnectionStringSettings["connString"]);

conn.Open();

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = ”select * from Customers”;
da.SelectCommand.Connection = conn;

da.Fill(ds);


I am using the default Northwind database for this example, once the DataSet has got filled in with returned records, you can use DataBind method to bind data to various ASP.NET Web Controls(DataGrid, DropDownList, RadioButtonList etc). I will cover more on ASP.NET Web Controls in the coming posts.

Series
PHP VS ASP.NET - The Beginning.
PHP VS ASP.NET - The Databases.

Share and Enjoy:
  • del.icio.us
  • digg
  • Reddit
  • YahooMyWeb

Tue, March 20 2007 » PHP VS ASP.NET

One Response

  1. Ayyoob April 14 2007 @ 11:42 am

    Hello,
    Here is an interesting article on PHP vs ASP. Good luck to you.

Leave a Reply