By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,393 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Database class how to write it ?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
> 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 discussion thread is closed

Replies have been disabled for this discussion.