473,397 Members | 2,077 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,397 software developers and data experts.

Please fire at this class...

Hi,

To make data-life a little easier, and not having to declare a
SqlConnection on each page, I came up with this Sql-Data-class:

' ---------------------------------

using System;
using System.Data;
using System.Data.SqlClient;

namespace MyFirstMaybeLeakyClasses
{
public class Data {

public Data()
{
oConn.Open()
}

private static SqlConnection oConn;

public static SqlConnection Connection {
get {
if (oConn == null)
oConn = new SqlConnection([connectionstring));

if (oConn.State != ConnectionState.Open)
oConn.Open();

return oConn;
}
}
}
}

' ---------------------------------

What are the cons of using a SqlConnection like this (as a static)? And
should I use it like this at all?

I understood each user-request will instantiate a new connection, so
multiple readers on the same connection shouldn't be a problem. Or am I
mistaking?
From time to time it throws a SqlException stating multiple
SqlDataReaders are trying to use the same connection. Eventhough I'm
using a DataAdapter... I suspect this happens whenever more that one
user is requesting the page(s).

Finally I have this problem that sometimes it complains it can't get a
connection from the connectionpool. I am closing the connection at the
end of each page. Does a connectionclose take several minutes to fully
terminate the connection or could it be something else?

Thanks in advance!
Apr 4 '06 #1
5 1824
WHOOOA!

Let's... just step away from the keyboard for a moment... What you've
got there is a single connection. Declaring it as static means that it
is shared amongst all instances of that class. If one instance of that
class modifies the connection, all other instances are talking to the
same connection. If someone opens the connection to use in a query,
nobody else can use it.

Why did you do this? Why do you want to declare a SqlConnection on a
page _at all_? Your pages really shouldn't know anything about
SqlConnections or any other of them thar fancy data access classes.

Apr 4 '06 #2
That said, your first sentence gives me hope, Sjaakie: "To make
data-life a little easier, and not having to declare a SqlConnection on
each page, I came up with this Sql-Data-class"

Okay, that's good, really good. Keep your data access code in data
access classes and return business objects from those classes. Accept
business objects as parameters to your save methods. Make sure you're
using try/finally to guarantee the connection is closed like

try
{
cn.open();
cmd.ExecuteNonQuery();
}
finally
{
cn.close();
}
Now your data access code is all nice and neat and out your pages, and
you can use it elsewhere. I'm presuming you can make this data access
class static but I've always been wary of doing so out of sheer
ignorance re:threading (anyone care to enlighten me while we're here?).
I generally use the singleton pattern.

Apr 4 '06 #3
Flinky Wisty Pomm schreef:
That said, your first sentence gives me hope, Sjaakie: "To make
data-life a little easier, and not having to declare a SqlConnection on
each page, I came up with this Sql-Data-class"

Okay, that's good, really good. Keep your data access code in data
access classes and return business objects from those classes. Accept
business objects as parameters to your save methods. Make sure you're
using try/finally to guarantee the connection is closed like

try
{
cn.open();
cmd.ExecuteNonQuery();
}
finally
{
cn.close();
}
Now your data access code is all nice and neat and out your pages, and
you can use it elsewhere. I'm presuming you can make this data access
class static but I've always been wary of doing so out of sheer
ignorance re:threading (anyone care to enlighten me while we're here?).
I generally use the singleton pattern.


As you might have noticed, I'm somewhat noob in developing OO-applications.

What I'm trying to achieve here is a Data-class which opens a connection
which can be used throughout the entire page/request and is closed at
the end. To me, this looks faster than opening and closing a connection
for each query.

Can you advice me or perhaps point me to a properly written data-layer
example which generally does what I'm looking for?

Thanks

Apr 4 '06 #4
"Sjaakie" <ke**@secret.it> wrote in message
news:44***********************@news.xs4all.nl...
Can you advice me or perhaps point me to a properly written data-layer
example which generally does what I'm looking for?


http://aspnet.4guysfromrolla.com/articles/070203-1.aspx
Apr 4 '06 #5
Mark Rae schreef:
"Sjaakie" <ke**@secret.it> wrote in message
news:44***********************@news.xs4all.nl...
Can you advice me or perhaps point me to a properly written data-layer
example which generally does what I'm looking for?


http://aspnet.4guysfromrolla.com/articles/070203-1.aspx


Thanks!
Apr 4 '06 #6

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

Similar topics

1
by: BC | last post by:
Hi all, I have two classes: TransactionCollection and Transaction, where TransactionCollection is used to store Transaction objects. public class TransactionCollection : CollectionBase {...
6
by: srallen | last post by:
I have a simple page that has a dropdown with autopostback=true and onselectedindexchanged is set to a function that never gets called (checked during debug). This page is subclassed from custom...
28
by: Tim_Mac | last post by:
hi, i'm new to .net 2.0, and am just starting to get to grips with the gridview. my page has autoEventWireUp set to true, which i gather is supposed to figure out which handlers to invoke when...
4
by: Ty Salistean | last post by:
So, here is a wierd question that we have been discussing for a bit now. Does an event fire even though nothing is subscribed to listen to the event? For instance, does the Click event of a...
8
by: John Austin | last post by:
I need to understand why if I add a control and use AddHandler to connect its click event, it will work in Page_Load, but not in a Button_Click. The idea is that the user types some data, presses...
20
by: C# Beginner | last post by:
I'm currently creating a database class, which contains a member called Open(). With this method users can open different databases. When a user tries to open a database which happens to be secured...
6
by: sjoshi | last post by:
I have a derived class OraBackup which has a method that calls stored procedure on Oracledb to get status of backup job. Now the base class publishes an event like this: public delegate void...
2
by: Joergen Bech | last post by:
Hope someone has a solution or some suggestions for this. This cannot be right?!? Problem: I have multiple non-modal forms open at the same time. One or more of these forms have a...
6
by: Simon Harvey | last post by:
Hi all, I'm really hoping someone can help me with this as it's causing me some serious problems. I have a Windows Forms application using the gridview control. When the user selects a row,...
3
by: Crazy Cat | last post by:
Hi all, I am developing an asp.net 2.0 application in Visual Studio 2005. On my page I have a simple datalist that is bound programmatically to a collection of simple objects. On this page I...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.