Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Connection to database

Reply
Thread Tools

Connection to database

 
 
RedRed
Guest
Posts: n/a
 
      03-06-2005
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
 
Reply With Quote
 
 
 
 
Manohar Kamath
Guest
Posts: n/a
 
      03-06-2005
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" <> wrote in message
news:4e0201c52249$8997a160$...
> 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



 
Reply With Quote
 
 
 
 
=?Utf-8?B?VmlzaG51LUNoaXZ1a3VsYQ==?=
Guest
Posts: n/a
 
      03-06-2005
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:

> 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
>

 
Reply With Quote
 
Manohar Kamath
Guest
Posts: n/a
 
      03-06-2005
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" <> wrote in
message news:CB7856F3-0968-4DF3-A86A-...
> 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:
>
> > 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
> >



 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      03-06-2005
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" <> wrote in
message news:CB7856F3-0968-4DF3-A86A-...
> 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:
>
>> 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
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Database Connection - Not Associated With Trusted Connection Mythran ASP .Net 5 10-05-2005 10:50 PM
Re: Software Developer to transcribe Oracle database into Access database Fluker MCSD 0 07-09-2003 01:20 PM
Re: Software Developer to transcribe Oracle database into Access database Guy Cox MCSD 1 07-09-2003 08:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57