System.Data.Odbc.OdbcDataReader.OdbcDataReader.GetBytes C# (CSharp) Method

GetBytes() public method

public GetBytes ( int i, long dataIndex, byte buffer, int bufferIndex, int length ) : long
i int
dataIndex long
buffer byte
bufferIndex int
length int
return long
		long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
		{
			if (IsClosed)
				throw new InvalidOperationException ("Reader is not open.");
			if (currentRow == -1)
				throw new InvalidOperationException ("No data available.");

			OdbcReturn ret = OdbcReturn.Error;
			bool copyBuffer = false;
			int returnVal = 0, outsize = 0;
			byte [] tbuff = new byte [length+1];

			if (buffer == null)
				length = 0;
			ret=libodbc.SQLGetData (hstmt, (ushort) (i + 1), SQL_C_TYPE.BINARY, tbuff, length, 
						ref outsize);

			if (ret == OdbcReturn.NoData)
				return 0;

			if ( (ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
				throw Connection.CreateOdbcException (OdbcHandleType.Stmt, hstmt);

			OdbcException odbcException = null;
			if ( (ret == OdbcReturn.SuccessWithInfo))
				odbcException = Connection.CreateOdbcException (
					OdbcHandleType.Stmt, hstmt);

			if (buffer == null)
				return outsize; //if buffer is null,return length of the field

			if (ret == OdbcReturn.SuccessWithInfo) {
				if (outsize == (int) OdbcLengthIndicator.NoTotal)
					copyBuffer = true;
				else if (outsize == (int) OdbcLengthIndicator.NullData) {
					copyBuffer = false;
					returnVal = -1;
				} else {
					string sqlstate = odbcException.Errors [0].SQLState;
					//SQLState: String Data, Right truncated
					if (sqlstate != libodbc.SQLSTATE_RIGHT_TRUNC)
						throw odbcException;
					copyBuffer = true;
				}
			} else {
				copyBuffer = outsize == -1 ? false : true;
				returnVal = outsize;
			}

			if (copyBuffer) {
				if (outsize == (int) OdbcLengthIndicator.NoTotal) {
					int j = 0;
					while (tbuff [j] != libodbc.C_NULL) {
						buffer [bufferIndex + j] = tbuff [j];
						j++;
					}
					returnVal = j;
				} else {
					int read_bytes = Math.Min (outsize, length);
					for (int j = 0; j < read_bytes; j++)
						buffer [bufferIndex + j] = tbuff [j];
					returnVal = read_bytes;
				}
			}
			return returnVal;
		}