Friday, July 20, 2007

How to Create DataBase Connection Using WebConfig for .Net

For Access
===========


In WebConfig
============
Rules
=====

1.Within a Configuration tag u can palce it anywhere.
2.This <connectionStrings> or <appSettings> tag shoul not be nested with any other tag other than this <configuration>

Within the configuration tag they are 2 diff way of connection,they are
==============================================================

1st way
========

Include the below content in webconfig
======================================
<appSettings>
<remove key ="Con"/>
<add key ="Con" value ="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Kanna\Test.mdb" />
</appSettings>



In Code Behind
=================

public partial class Default3 : System.Web.UI.Page
{
OleDbConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{

Connect = new OleDbConnection(ConfigurationManager.AppSettings["Con"]);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
}

=======================================================================================


2nd Way
=======

Include the below content in webconfig
======================================
<connectionStrings>
<remove name ="Con" />
<add name ="Con" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Kanna\Test.mdb" providerName="System.Data.OleDb" />
</connectionStrings>



In Code Behind
=================

public partial class Default3 : System.Web.UI.Page
{
OleDbConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{

Connect = new OleDbConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
}

=========================================================================================



For Oracle
==========

1st way
========

Include the below content in webconfig
======================================
<appSettings>
<remove key ="Con"/>
<add key ="Con" value ="data source=tcms; User id=tcms_testnew; password=socool;" />
</appSettings>



In Code Behind
=================

public partial class Default3 : System.Web.UI.Page
{
OracleConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{

Connect = new OracleConnection (ConfigurationManager.AppSettings["Con"]);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OracleDataAdapter adp = new OracleDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
}

=======================================================================================


2nd Way
=======

Include the below content in webconfig
======================================
<connectionStrings>
<remove name="Con"/>
<add name="Con" connectionString="data source=tcms; User id=tcms_testnew; password=socool;" providerName="System.Data.OracleClient"/>
</connectionStrings>



In Code Behind
=================

public partial class Default3 : System.Web.UI.Page
{
OracleConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{

Connect = new OracleConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OracleDataAdapte adp = new OracleDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
}

===========================================================================================

No comments:

 
Feedback Form