473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Connection to database

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
Nov 19 '05 #1
4 1096
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" <an*******@discussions.microsoft.com> wrote in message
news:4e****************************@phx.gbl...
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

Nov 19 '05 #2
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

Nov 19 '05 #3
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" <Vi*************@discussions.microsoft.com> wrote in
message news:CB**********************************@microsof t.com...
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

Nov 19 '05 #4
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" <Vi*************@discussions.microsoft.com> wrote in
message news:CB**********************************@microsof t.com...
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

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: AMC | last post by:
Hi, I'm using an include file to store the connection string to a database. Whenever I try to reference that string to open a connection in the page that includes the file I get the error 'empy...
11
by: pradeep_TP | last post by:
Hi all, I have a few questions that I have been wanting to ask for long. These are all related to ADO.net and specifically to conenction to database. 1) If I have opened a connection to a...
3
by: R Reyes | last post by:
Hi, I'm trying to modularize my database connections a little better and get more out of my project with less code. First check out this common dbOpen() function inside class clsDatabase. I...
7
by: Lau Lei Cheong | last post by:
Hello, Actually I think I should have had asked it long before, but somehow I haven't. Here's the scenerio: Say we have a few pages in an ASP.NET project, each of them needs to connect to...
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
6
by: Arsalan Ahmad | last post by:
Hi all, I am creating a website in which in an Item detail page there are a number of web controls (7 or 8) and what is happening that inside each of control's Page_Load() function I am creating...
5
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
6
Cintury
by: Cintury | last post by:
Hi all, I've developed a mobile application for windows mobile 5.0 that has been in use for a while (1 year and a couple of months). It was developed in visual studios 2005 with a back-end sql...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.