473,320 Members | 1,952 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,320 software developers and data experts.

Database class how to write it ?

Hey
I would like to write a database class. Let's say it would work like this:
datareader GetDataReader(string sqlquery);
This would be static, so I could do this:

datareader dr = myclass.GetDataReader("SELECT * FROM table);
But in this function I would have an open connection how to close it ?
I return dr, does it create a new object or gives me a reffernce to existing
one ?
It's done like this : return dr; so ... ? Maybe you will give me some light
on this subject.
Jarod.Net
Nov 16 '05 #1
3 1720
Jarod wrote:
Hey
I would like to write a database class. Let's say it would work like this:
datareader GetDataReader(string sqlquery);
This would be static, so I could do this: [ I don't understand why do you want it be static, I would suggest that
when you wrap a class on the db connection, you may not want it stati]

You can do something like this:

class CDBWrapper
{
public :
OpenConnection();
GetDataReader(string sqlquery);
CloseConnection();
}

Also read some dispose pattern on this topic in MSDN on how to ensure
that connection will be properly closed. There are plenty of information
on that.

datareader dr = myclass.GetDataReader("SELECT * FROM table);
But in this function I would have an open connection how to close it ?
I return dr, does it create a new object or gives me a reffernce to existing
one ?
[ I think it will return a reference to existing one, that's why it
normally do when you pass objects around in .Net ] It's done like this : return dr; so ... ? Maybe you will give me some light
on this subject.
Jarod.Net

Nov 16 '05 #2
Hi Jarod,

I would suggest that you take a look at the Microsoft Data Access
Application Block (DAAB) and examine their implementation. What it does is
pass a parameter stating if the client opened the connection leave it to the
client to manage if the class opened the connection the class should close
it. You can download it at:

http://msdn.microsoft.com/library/de.../html/daab.asp

I hope this helps.
-------------------------------
Nov 16 '05 #3
> class CDBWrapper
{
public :
OpenConnection();
GetDataReader(string sqlquery);
CloseConnection();
}


I found out that if I set
SqlCommand.ExecuteReader(CommandBehavior.CloseConn ection);
Then on the returned DataReader.Close closes also connection ;)
But for the SqlCommand I will probably use your's solution.
Jarod.Net
Nov 16 '05 #4

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

Similar topics

2
by: | last post by:
I am hoping a mixed ASP- Javascript programmer could help me with this scenario: I have a list box populated with randomized records (NewID()) from a Broker (Salesperson) sql server table. ...
9
by: Chris | last post by:
Hi all, I wrote a cool little database program a while back, in VB6, and im intending to rewrite it in .net. I am new(ish) to .net, but an old hand at VB5/6. In VB i would access the mdb...
6
by: btopenworld | last post by:
I have an access database with lots of fields including a memo field called 'priecinfo'. Everything works well, lots of data is written, but in one particular situation, I can't get the field...
3
by: .Net Newbie | last post by:
I'm new to .Net and need to create a generic (free) way to update lookup tables in SQL Server (using C#) in ASP.Net pages. I found an article at:...
1
by: dave | last post by:
I'm classic ASP developer and trying to switch to .net ...I'm newbie to ..net... In classic ASP for ADO connectivity i used to put below code in one separate file and used to include that file...
0
bartonc
by: bartonc | last post by:
With one small change to the view/control: self.staticText3 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT3, label='ODBC Data Source Name', name='staticText3',...
5
by: smileskhan | last post by:
Hay Friends... Here I started a new and interested thread. I hope you also enjoy it. I got a task to creat a Hospital Database in Java. But I donīt have any good clues abouts it....
1
by: pontypete | last post by:
Hi all Ive managed to create an ASP & ACCESS local site/databse, Now the aim: Membership Number = 1 Fname = Peter ^ everything in the databse, I want to beable to have a search box within...
2
by: gnomee2 | last post by:
Hello Everyone, I have a strange problem that I cannot seem to solve. I have two server running Windows 2003 MSSQL on one IIS on the other. Out of the blue I have slow queries that cause asp...
12
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I am a newbie to Struts and JSP...I have been working on the code below for 5 hours now..I googled a lot but couldn't get much help so finally I am here.. Hoping of getting my problem solved. Please...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.