changing variable types of a function of a derived class

  • Thread starter Thread starter mrclash
  • Start date Start date
M

mrclash

Hello,

I have a class that uses some variables to access a sql database, and I
want to make a derived class that inherits all the methods of this
class, because the derived one will do exactly the same process that
the parent one, but with a postgres database, thus I've got to use
diferent type variables to acces the database.

There's one big function in the class that does all the DB stuff, and I
want to know if there's any way to inherit the function but with only
changing the type of some vars inside this function.

Thanks!

Sergi
 
Sergi

Is this what your trying to achieve...

abstract class MyBaseClass
{
private IDbConnection _connection;
public virtual IDbConnection Connection
{
get { return _connection; }
set { _connection = value; }
}

public MyBaseClass()
{
Connection = CreateConnection();
}

protected abstract IDbConnection CreateConnection();

public virtual void DoSomeSql()
{
if ( Connection.State != ConnectionState.Open )
{
Connection.Open();
}
using ( IDbCommand command = Connection.CreateCommand() )
{
//If it's parameterised sql you'll need to check what the placeholders
should be for the DB type
//@=sql server, ?=odbc, :=oracle
command.CommandText = "SELECT * FROM T_SOME_DATA";

using ( IDataReader reader = command.ExecuteReader() )
{
while ( reader.Read() )
{
//DoSomething
}
}
}
}
}
class MyOracleVersion : MyBaseClass
{
protected override IDbConnection CreateConnection()
{
return new OracleConnection( "Data Source=emp;user
id=scott;password=tiger" );
}
}
class MySqlServerVersion : MyBaseClass
{
protected override IDbConnection CreateConnection()
{
return new SqlConnection( "How should I know this, I use Oracle" );
}
}

HTH

Glenn
 
Yeah, this is it! I didn't knew of the IDbConnection interface.

Thanks a lot, and thanks to answer such a newbie question.

Sergi
 

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