Database class how to write it ?

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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 ]
 
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

Back
Top