public void GetCharsTest() { _dt.Columns.Add("col2", typeof(char[])); _dt.Rows.Clear(); _dt.Rows.Add(new object[] { 1, "string", "string".ToCharArray() }); _dt.Rows.Add(new object[] { 2, "string1", null }); DataTableReader rdr = _dt.CreateDataReader(); rdr.Read(); // Unable to cast object of type 'System.String' to type 'System.Char[]'. Assert.Throws <InvalidCastException>(() => rdr.GetChars(1, 0, null, 0, 10)); char[] char_arr = null; long len = 0; len = rdr.GetChars(2, 0, null, 0, 0); Assert.Equal(6, len); char_arr = new char[len]; len = rdr.GetChars(2, 0, char_arr, 0, 0); Assert.Equal(0, len); len = rdr.GetChars(2, 0, null, 0, 0); char_arr = new char[len + 2]; len = rdr.GetChars(2, 0, char_arr, 2, 100); Assert.Equal(6, len); char[] val = (char[])rdr.GetValue(2); for (int i = 0; i < len; ++i) { Assert.Equal(val[i], char_arr[i + 2]); } }
private static void TestGetChars(DataTableReader reader, int bufferSize) { // The filename is in column 0, and the contents are in column 1. const int FILENAME_COLUMN = 0; const int DATA_COLUMN = 1; char[] buffer; long offset; int charsRead = 0; string fileName; int currentBufferSize = 0; while (reader.Read()) { // Reinitialize the buffer size and the buffer itself. currentBufferSize = bufferSize; buffer = new char[bufferSize]; // For each row, write the data to the specified file. // First, verify that the FileName column isn't null. if (!reader.IsDBNull(FILENAME_COLUMN)) { // Get the file name, and create a file with // the supplied name. fileName = reader.GetString(FILENAME_COLUMN); // Start at the beginning. offset = 0; using (StreamWriter outputStream = new StreamWriter(fileName, false)) { try { // Loop through all the characters in the input field, // incrementing the offset for the next time. If this // pass through the loop reads characters, write them to // the output stream. do { charsRead = (int)reader.GetChars(DATA_COLUMN, offset, buffer, 0, bufferSize); if (charsRead > 0) { outputStream.Write(buffer, 0, charsRead); offset += charsRead; } } while (charsRead > 0); } catch (Exception ex) { Console.WriteLine(fileName + ": " + ex.Message); } } } } Console.WriteLine("Press Enter key to finish."); Console.ReadLine(); }
public void GetCharsTest() { _dt.Columns.Add("col2", typeof(char[])); _dt.Rows.Clear(); _dt.Rows.Add(new object[] { 1, "string", "string".ToCharArray() }); _dt.Rows.Add(new object[] { 2, "string1", null }); DataTableReader rdr = _dt.CreateDataReader(); rdr.Read(); try { rdr.GetChars(1, 0, null, 0, 10); Assert.False(true); } catch (InvalidCastException e) { // Never premise English. //Assert.Equal ("Unable to cast object of type 'System.String'" + // " to type 'System.Char[]'.", e.Message, "#1"); } char[] char_arr = null; long len = 0; len = rdr.GetChars(2, 0, null, 0, 0); Assert.Equal(6, len); char_arr = new char[len]; len = rdr.GetChars(2, 0, char_arr, 0, 0); Assert.Equal(0, len); len = rdr.GetChars(2, 0, null, 0, 0); char_arr = new char[len + 2]; len = rdr.GetChars(2, 0, char_arr, 2, 100); Assert.Equal(6, len); char[] val = (char[])rdr.GetValue(2); for (int i = 0; i < len; ++i) { Assert.Equal(val[i], char_arr[i + 2]); } }
public long GetChars(int i, long fieldOffset, char[] buffer, int bufferOffset, int length) { return(_reader.GetChars(i, fieldOffset, buffer, bufferOffset, length)); }