long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
{
if ((command.CommandBehavior & CommandBehavior.SequentialAccess) != 0) {
ValidateState ();
EnsureDataAvailable ();
if (i < 0 || i >= command.Tds.Columns.Count)
throw new IndexOutOfRangeException ();
Encoding encoding = null;
byte mul = 1;
TdsColumnType colType = (TdsColumnType) command.Tds.Columns[i]["ColumnType"];
switch (colType) {
case TdsColumnType.Text :
case TdsColumnType.VarChar:
case TdsColumnType.Char:
case TdsColumnType.BigVarChar:
encoding = Encoding.ASCII;
break;
case TdsColumnType.NText :
case TdsColumnType.NVarChar:
case TdsColumnType.NChar:
encoding = Encoding.Unicode;
mul = 2;
break;
default :
return -1;
}
long count = 0;
if (buffer == null) {
count = GetBytes (i,0,(byte[]) null,0,0);
return (count/mul);
}
length *= mul;
byte[] arr = new byte [length];
count = GetBytes (i, dataIndex, arr, 0, length);
if (count == -1)
throw new InvalidCastException ("Specified cast is not valid");
Char[] val = encoding.GetChars (arr, 0, (int)count);
val.CopyTo (buffer, bufferIndex);
return val.Length;
}
char [] valueBuffer;
object value = GetValue (i);
if (value is char[])
valueBuffer = (char[])value;
else if (value is string)
valueBuffer = ((string)value).ToCharArray();
else {
if (value is DBNull) throw new SqlNullValueException ();
throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
}
if ( buffer == null ) {
// Return length of data
return valueBuffer.Length;
}
else {
// Copy data into buffer
Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
return valueBuffer.Length - dataIndex;
}
}