SqlDataReaderを実装しているためIDataReader、GetChar()メソッドがあります。しかし、SqlDataReaderでは、その実装は単にNotImplementedException. はインテリセンスでSqlDataReader提供しないようにマークされていますが、 の最後に入力すると、コンパイルされていることがわかりますが、実行時に.GetChar()GetChar()SqlDataReaderNotImplementedException
.Net チームが .NET を実装するのに数行のコードしかかからなかったと思われるので、すべてがかなり残念ですGetChar()。
幸いなことに、拡張メソッドを使用して、独自のGetChar()メソッドを に追加できますSqlDataReader。既に使用されているようGetChar()に ( でのみ実装されていますがNotImplmentedException)、 . 以外の名前を付ける必要がありGetChar()ます。私はそれを呼んだGetSingleChar():
internal static class ExtensionMethods
{
internal static char GetSingleChar(this SqlDataReader reader, int columnIndex)
{
System.Data.SqlTypes.SqlChars val = reader.GetSqlChars(columnIndex);
if (val.Length != 1)
{
throw new ApplicationException(
"Expected value to be 1 char long, but was "
+ val.Length.ToString() + " chars long.");
}
return val[0];
}
}