Connecting Tech Pros Worldwide Forums | Help | Site Map

Connection to database

RedRed
Guest
 
Posts: n/a
#1: Nov 19 '05
Hi all,

Hi to connect to database if I want to write the code in
the class instead in the html (.aspx). What I want to do
is to create a reusable class which allow the other page
to connect using one class.

Thanks all

Manohar Kamath
Guest
 
Posts: n/a
#2: Nov 19 '05

re: Connection to database


I think you have answered this yourself -- add a class to your project,
which connects to the database, and perhaps returns a connection object that
all pages can re-use.


// Define a class
public class MyConn
{
// This is the internal member to hold connection
SqlConnection _conn = null;

// Create a connection in the constructor
public MyConn()
{
// Create a connection in this constructor
}

// This is the public property
public SqlConnection Connection
{
get
{
return _conn;
}
}
}

Hope that helps.

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com


"RedRed" <anonymous@discussions.microsoft.com> wrote in message
news:4e0201c52249$8997a160$a401280a@phx.gbl...[color=blue]
> Hi all,
>
> Hi to connect to database if I want to write the code in
> the class instead in the html (.aspx). What I want to do
> is to create a reusable class which allow the other page
> to connect using one class.
>
> Thanks all[/color]


Vishnu-Chivukula
Guest
 
Posts: n/a
#3: Nov 19 '05

re: Connection to database


Hi,

As Manohar pointed out, you can add a class to your project and proceed.


Another way would be to create a wrapper class for the methods provided by
the framework. Make an assembly (a .dll file) and reference it. This is in
case you have a 3 tier architecture and needs to access the data access
methods a lot..

A sample can be like this...

Create a static class like DataBase and have a private static connection
string

private static string sConnectionString = The connection string

You can then have methods like this for filling up datatable / dataset. You
can also have for ExecuteNonQuery and any others you need.

public static DataTable ExecuteDataTable(SqlCommand command)
{
using (SqlDataAdapter da = new SqlDataAdapter(command))
{
command.Connection = GetConnection();
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}

Just another approach...

HTH,

Need any help, do post a msg back..


Happy coding

"RedRed" wrote:
[color=blue]
> Hi all,
>
> Hi to connect to database if I want to write the code in
> the class instead in the html (.aspx). What I want to do
> is to create a reusable class which allow the other page
> to connect using one class.
>
> Thanks all
>[/color]
Manohar Kamath
Guest
 
Posts: n/a
#4: Nov 19 '05

re: Connection to database


Good idea, also: Instead of using a static variable, store the connection
string in web.config to make it portable, and if need be encrypt it using
asp setreg utility.

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com


"Vishnu-Chivukula" <VishnuChivukula@discussions.microsoft.com> wrote in
message news:CB7856F3-0968-4DF3-A86A-0FB401ED1724@microsoft.com...[color=blue]
> Hi,
>
> As Manohar pointed out, you can add a class to your project and proceed.
>
>
> Another way would be to create a wrapper class for the methods provided by
> the framework. Make an assembly (a .dll file) and reference it. This is in
> case you have a 3 tier architecture and needs to access the data access
> methods a lot..
>
> A sample can be like this...
>
> Create a static class like DataBase and have a private static connection
> string
>
> private static string sConnectionString = The connection string
>
> You can then have methods like this for filling up datatable / dataset.[/color]
You[color=blue]
> can also have for ExecuteNonQuery and any others you need.
>
> public static DataTable ExecuteDataTable(SqlCommand command)
> {
> using (SqlDataAdapter da = new SqlDataAdapter(command))
> {
> command.Connection = GetConnection();
> DataTable dt = new DataTable();
> da.Fill(dt);
> return dt;
> }
> }
>
> Just another approach...
>
> HTH,
>
> Need any help, do post a msg back..
>
>
> Happy coding
>
> "RedRed" wrote:
>[color=green]
> > Hi all,
> >
> > Hi to connect to database if I want to write the code in
> > the class instead in the html (.aspx). What I want to do
> > is to create a reusable class which allow the other page
> > to connect using one class.
> >
> > Thanks all
> >[/color][/color]


Juan T. Llibre
Guest
 
Posts: n/a
#5: Nov 19 '05

re: Connection to database


That sounds fine.

I'd be wary of calling it a "wrapper", though,
to prevent confusion between that and Interop.




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Espaņol
Ven, y hablemos de ASP.NET...
======================

"Vishnu-Chivukula" <VishnuChivukula@discussions.microsoft.com> wrote in
message news:CB7856F3-0968-4DF3-A86A-0FB401ED1724@microsoft.com...[color=blue]
> Hi,
>
> As Manohar pointed out, you can add a class to your project and proceed.
>
>
> Another way would be to create a wrapper class for the methods provided by
> the framework. Make an assembly (a .dll file) and reference it. This is in
> case you have a 3 tier architecture and needs to access the data access
> methods a lot..
>
> A sample can be like this...
>
> Create a static class like DataBase and have a private static connection
> string
>
> private static string sConnectionString = The connection string
>
> You can then have methods like this for filling up datatable / dataset.
> You
> can also have for ExecuteNonQuery and any others you need.
>
> public static DataTable ExecuteDataTable(SqlCommand command)
> {
> using (SqlDataAdapter da = new SqlDataAdapter(command))
> {
> command.Connection = GetConnection();
> DataTable dt = new DataTable();
> da.Fill(dt);
> return dt;
> }
> }
>
> Just another approach...
>
> HTH,
>
> Need any help, do post a msg back..
>
>
> Happy coding
>
> "RedRed" wrote:
>[color=green]
>> Hi all,
>>
>> Hi to connect to database if I want to write the code in
>> the class instead in the html (.aspx). What I want to do
>> is to create a reusable class which allow the other page
>> to connect using one class.
>>
>> Thanks all
>>[/color][/color]


Closed Thread