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

Enterprise library database question

I'm using the EL 2.0 library. I have a datalayer where I am debating doing 2
things... I'm concerned that because I am using a IDataReader, my connection
to the database is never getting closed and I have to rely on garbage
collection to close the connections.

I came across an article that said if I do a .Dispose() on the DbCommand
object, the connection will be closed... but I don't think that is true,
because the IDataReader that I am passing back from my data layer to my
Business Rules layer would fail if the connection was closed.

In my first example (EXAMPLE 1), I return an IDataReader and do the .Dispose
on my DbCommand object
EXAMPLE 1 QUESTION - is that ok?

In my second example (EXAMPLE 2), I return an object that I created instead
of the IDataReader and I close the IDataReader right away

EXAMPLE 2 QUESTION - is that a better idea? I don't like having my Core
object layer involved in my data layer, but I think I might have to do it
this way.
EXAMPLE 2 QUESTION - other than closing the IDataReader, do I have to
close/dispose anything else? I believe I read somewhere that when the
IDataReader is closed, the connection is closed. Do I need to do anything
with the connection, or the DbCommand object?

EXAMPLE 1
#region SelectVehicleSoldByDealership
public IDataReader SelectVehicleSoldByDealership(long passedIn_DS_NUMBER,
string passedIn_VEH_STATUS)
{
IDataReader iDataReader = null;

try
{
DbCommand dbCommand = _commonDatabase.GetStoredProcCommand
("VEHICLE_SELECT_SOLD_BY_DEALER");

//Parameters
_commonDatabase.AddInParameter(dbCommand, "DS_NUMBER", DbType.Int32,
passedIn_DS_NUMBER);
_commonDatabase.AddInParameter(dbCommand, "VEH_STATUS", DbType.String,
passedIn_VEH_STATUS);
iDataReader = _commonDatabase.ExecuteReader(dbCommand);
}
catch (Exception ex)
{
errorHelper = new ErrorHelper();
string errorMessage = errorHelper.FormatError(ex);
ExceptionPolicy.HandleException(new Exception(string.Format("Error: {0}",
errorMessage)), "Log And Email");

iDataReader = null;
}
finally
{
if (dbCommand != null)
dbCommand .Dispose();
}

return iDataReader ;
}
#endregion SelectVehicleSoldByDealership
EXAMPLE 2
#region SelectVehicleSoldByDealership
public List<VehicleRecordSelectVehicleSoldByDealership(lo ng
passedIn_DS_NUMBER,
string passedIn_VEH_STATUS)
{
IDataReader iDataReader = null;
List<VehicleRecordvehicleRecordList = new List<VehicleRecord>();
VehicleRecord vehicleRecord = null;

try
{
DbCommand dbCommand = _commonDatabase.GetStoredProcCommand
("VEHICLE_SELECT_SOLD_BY_DEALER");

//Parameters
_commonDatabase.AddInParameter(dbCommand, "DS_NUMBER", DbType.Int32,
passedIn_DS_NUMBER);
_commonDatabase.AddInParameter(dbCommand, "VEH_STATUS", DbType.String,
passedIn_VEH_STATUS);
iDataReader = _commonDatabase.ExecuteReader(dbCommand);

while (iDataReader.Read())
{
vehicleRecord = new VehicleRecord();

vehicleRecord.StockNumber = iDataReader["VEH_STOCK_NUMBER"].ToString();
vehicleRecord.Year = Convert.ToInt32(iDataReader["VEH_YEAR"]);
vehicleRecord.Make = iDataReader["VEH_MAKE"].ToString();
vehicleRecord.Model = iDataReader["VEH_MODEL"].ToString();
vehicleRecordList.Add(vehicleRecord);
}

}
catch (Exception ex)
{
errorHelper = new ErrorHelper();
string errorMessage = errorHelper.FormatError(ex);
ExceptionPolicy.HandleException(new Exception(string.Format("Error: {0}",
errorMessage)), "Log And Email");

vehicleRecordList = null;
}
finally
{
if (iDataReader != null)
iDataReader.Close();
}

return vehicleRecordList;
}
#endregion SelectVehicleSoldByDealership
Sep 8 '07 #1
1 1967
There is an overload of ExecuteReader that accepts a CommandBehavior;
one of the options here is CloseConnection, which when specified means
that closing the reader (not the command) will also close the
connection, which makes sense if you only want the connection while
you are reading data. I imagine (I haven't checked) that the
enterprise library uses this option, since IIRC it masks the
connection from you.

Re intermingling the core objects with your data-layer... isn't that
kinda the job of the (project specific) data-layer? As for a non-
project specific layer... enterprise library of ADO.NET already fills
that hole. If you wanted to abstract this, you could do something
involving a delegate of some kind (Action<IDataReader>, perhaps) - but
I haven't thought about this enough to see if it is a good idea ;-p
However, this kind of "disconnected by the time you leave the method"
approach does have the advantage that you can Close()/Dispose()
everything at source.

Marc

Sep 8 '07 #2

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

Similar topics

1
by: Mike Chamberlain | last post by:
Hi all. I'm trying to extend the Microsoft Enterprise Library Data Access Application Block (http://msdn.microsoft.com/library/en-us/dnpag2/html/daab.asp?frame=true) to work with a Borland...
4
by: John Morgan | last post by:
I have Enterprise Manager on my local machine. For the last twelve months it has been connecting without problem to my online SQL Server database provided by my ISP. Three weeks ago the ISP...
0
by: srikash | last post by:
Not sure where to post this question as I didn't find any groups specific to enterprise library. So I'm posting here.. I'm trying to retrieve data from a DB2 database. It throws an Invalid...
8
by: poifull | last post by:
Is anyone using the Microsoft Enterprise Library? If yes, do you like it or not? Any feedback will be appreciated.
3
by: veera sekhar kota | last post by:
hi, im seriously looking for right answer .... We are developing windows application in c#. I implemented DAAB(Data Access Application Block) 2.0 in our application. One of the senior asked...
7
by: galico | last post by:
Hi All, We are having a very strange problem with the above. We have designed an application in ASP.NET 2.0 that uses the enterprise library data application blocks amongst others. We seem to be...
0
by: Chris Dunaway | last post by:
Enterprise Library January 2006 Visual Studio 2005 Lanugage: C# I am attempting to use the Enterprise Library to read data from an Excel spreadsheet file. I got the following connection...
1
by: rohan_from_mars | last post by:
Can anyone provide me with links or information and example on using database sink in Enterprise Library. I m using june 05 version and VS 2003. My main question is how and where do i provide...
2
by: rockdale | last post by:
Hi, all I am using Enterprise Library for .NET Framework 2.0 - January 2006 to access my backend MS SQL database. As now we are consider migrate sql database to mySQL. What engine (ODBC or...
1
by: rockdale | last post by:
Well, I guess I did express myself very clearly. I implemented the Ent Lib 2.0 data block against SQL database successfully. The system now is a production system. But now we want to migrate the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.