Thursday, November 5, 2009

A little surprise of a DbReader GetChar for System.Data.SqlClient and ODP.NET

Just a little thing I stumbled across today when trying to use OracleDataReader.GetChar – it is not actually supported. No ObsoleteAttribute, they just buff upon you with NotSupported exception:

public override char GetChar(int i)
{
    throw new NotSupportedException();
}

Good news is that GetString works for the char type just fine and you have GetChars method after all.

Out of curiosity I wanted to see if SqlClient.SqlDataReader has it implemented and surprisingly it doesn’t:

[EditorBrowsable(EditorBrowsableState.Never)]
public override char GetChar(int i)
{
    throw ADP.NotSupported();
}

This is actually the only method in SqlDataReader that does this thingy. In OracleDataReader though you have following methods sitting and waiting to give you a runtime error:

public DbDataReader GetData(int i)
{
    throw new NotSupportedException();
}
 
public override Guid GetGuid(int i)
{
    throw new NotSupportedException();
}

I have no idea what GetData is for, but no surprise GetGuid is not implemented in OracleClient, question though is – “Why not to put ObsoleteAttribute so I know I should not use those methods during compile time?”. There is a bit of a smell when using ObsoleteAttribute for that, but still I’d prefer to know ahead …

Post a Comment