Database class how to write it ?

G

Guest

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
 
J

Jianwei Sun

Jarod said:
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 ]
 
G

Guest

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/default.asp?url=/library/en-us/dnpag2/html/daab.asp

I hope this helps.
-------------------------------
 
G

Guest

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top