Is this Valid?

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

I have written the following method..

public IDataReader CreateDataReader(IDbCommand cmd)
{
if (ProviderType == 0)
{
OleCommand oCmd = (OleCommand) cmd;
OleDbDataReader dr = oCmd.ExecuteReader;
return (IDataReader) dr;
}
}

Would it be valid to write it like this?

public IDataReader CreateDataReader(IDbCommand cmd)
{
if (ProviderType == 0)
{
IDbDataReader dr = cmd.ExecuteReader;
return dr;
}
}

The IDbCommand being passed is ultimated either a OleDBCommand or a
SqlCommand - so is it valid to use the IDbCommand prior to changing it to
the appropriate class?

If that is valid, Since the type of object which is returned by the
cmd.ExecuteReader is either an OleDbDataReader or a SqlDataReader, must I
first get that and then "reclass" it to the IDataReader.

Thanks IN Advance for your assistance!!!!!!!!!!
 
Hi Jim,

Jim Heavey said:
I have written the following method..

public IDataReader CreateDataReader(IDbCommand cmd)
{
if (ProviderType == 0)
{
OleCommand oCmd = (OleCommand) cmd;
OleDbDataReader dr = oCmd.ExecuteReader;
return (IDataReader) dr;
}
}

Would it be valid to write it like this?

Yes, it would.
public IDataReader CreateDataReader(IDbCommand cmd)
{
if (ProviderType == 0)
{
IDbDataReader dr = cmd.ExecuteReader;
return dr;
}
}

The IDbCommand being passed is ultimated either a OleDBCommand or a
SqlCommand - so is it valid to use the IDbCommand prior to changing it to
the appropriate class?

Yes. Using interfaces just fires desired methods on instance.
If that is valid, Since the type of object which is returned by the
cmd.ExecuteReader is either an OleDbDataReader or a SqlDataReader, must I
first get that and then "reclass" it to the IDataReader.

No need. Since the classes supports the IDataReader interface is completely
safe to use it directly.
BTW, you have a typo (there is no IDbDataReader interface)
 

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