we can connect database in many ways in Asp.net. the main point is where we write the connection string. we can either write it in every .aspx.cs pages or in web config file or even in any global configuration file. we can connect database from every .aspx.cs page as:
string strConnection = @”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + Server.MapPath(@”~\App_Data\student.mdb”);
using (OleDbConnection con = new OleDbConnection(strConnection))
{
con.Open();
OleDbCommand cmd = new OleDbCommand(“select * from systemusers”, con);
OleDbDataReader rdr = cmd.ExecuteReader();
GridView1.DataSource = rdr;
GridView1.DataBind();
}
if we write the configuration in every aspx.cs pages(like above) then later time if we need to change database name then we have to change it over every pages. but we are not stupid. so we must write it in a common place like web.config.
by example,
web.config page contain:
<appSettings>
<add key=”ORACLEConnectionString” value=”Provider=OraOLEDB.Oracle.1;
Persist Security Info=False;Password=blah;User ID=greg;Data Source=sph;” />
<add key=”SQLConnectionString” value=”data source=SQL1;initial catalog=ID_V;
integrated security=SSPI;persist security info=False;workstation id=TH03D374;
packet size=4096″/>
<add key=”accessConnectionString” value=”Provider=Microsoft.Jet.OleDb.4.0; Data Source=|DataDirectory|student.mdb” />
</appSettings>
and aspx.cs file connect db as:
string strConnection = ConfigurationSettings.AppSettings["accessConnectionString"];
using (OleDbConnection con = new OleDbConnection(strConnection))
{
con.Open();
OleDbCommand cmd = new OleDbCommand(“select * from systemusers”, con);
OleDbDataReader rdr = cmd.ExecuteReader();
GridSystemUsers.DataSource = rdr;
GridSystemUsers.DataBind();
}
that’s it. central database configuration. @global file: still i am not good enough to write about.



