long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
{
if ((command.CommandBehavior & CommandBehavior.SequentialAccess) != 0) {
ValidateState ();
EnsureDataAvailable ();
try {
long len = ((Tds)command.Tds).GetSequentialColumnValue (i, dataIndex, buffer, bufferIndex, length);
if (len == -1)
throw CreateGetBytesOnInvalidColumnTypeException (i);
if (len == -2)
#if NET_2_0
throw new SqlNullValueException ();
#else
return 0;
#endif
return len;
} catch (TdsInternalException ex) {
command.Connection.Close ();
throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
}
}
object value = GetValue (i);
if (!(value is byte [])) {
SqlDbType type = GetSchemaRowDbType (i);
switch (type) {
case SqlDbType.Image:
if (value is DBNull)
throw new SqlNullValueException ();
break;
case SqlDbType.Text:
#if NET_2_0
string text = value as string;
if (text != null)
value = Encoding.Default.GetBytes (text);
else
value = null;
break;
#else
throw new InvalidCastException ();
#endif
case SqlDbType.NText:
#if NET_2_0
string ntext = value as string;
if (ntext != null)
value = Encoding.Unicode.GetBytes (ntext);
else
value = null;
break;
#else
throw new InvalidCastException ();
#endif
default:
throw CreateGetBytesOnInvalidColumnTypeException (i);
}
}
if (buffer == null)
return ((byte []) value).Length; // Return length of data
// Copy data into buffer
int availLen = (int) ( ( (byte []) value).Length - dataIndex);
if (availLen < length)
length = availLen;
if (dataIndex < 0)
return 0;
Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
return length; // return actual read count
}