1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 6 using System.Data.SqlTypes; 7 using Microsoft.SqlServer.Server; 8 using Xunit; 9 10 namespace System.Data.SqlClient.Tests 11 { 12 public class SqlDataRecordTest 13 { 14 15 [Fact] SqlRecordFillTest()16 public void SqlRecordFillTest() 17 { 18 SqlMetaData[] metaData = new SqlMetaData[] 19 { 20 new SqlMetaData("col1", SqlDbType.Bit), 21 new SqlMetaData("col2", SqlDbType.TinyInt), 22 new SqlMetaData("col3", SqlDbType.VarBinary, 1000), 23 new SqlMetaData("col4", SqlDbType.NVarChar, 1000), 24 new SqlMetaData("col5", SqlDbType.DateTime), 25 new SqlMetaData("col6", SqlDbType.Float), 26 new SqlMetaData("col7", SqlDbType.UniqueIdentifier), 27 new SqlMetaData("col8", SqlDbType.SmallInt), 28 new SqlMetaData("col9", SqlDbType.Int), 29 new SqlMetaData("col10", SqlDbType.BigInt), 30 new SqlMetaData("col11", SqlDbType.Real), 31 new SqlMetaData("col12", SqlDbType.Decimal), 32 new SqlMetaData("col13", SqlDbType.Money), 33 new SqlMetaData("col14", SqlDbType.Variant) 34 }; 35 36 SqlDataRecord record = new SqlDataRecord(metaData); 37 38 for (int i = 0; i < record.FieldCount; i++) 39 { 40 Assert.Equal($"col{i + 1}", record.GetName(i)); 41 } 42 43 record.SetBoolean(0, true); 44 Assert.Equal(true, record.GetBoolean(0)); 45 46 record.SetByte(1, 1); 47 Assert.Equal(1, record.GetByte(1)); 48 49 byte[] bb1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 50 byte[] bb2 = new byte[5]; 51 record.SetSqlBinary(2, new SqlBinary(new byte[0])); 52 record.SetBytes(2, 0, bb1, 0, 3); 53 record.SetBytes(2, 2, bb1, 6, 3); 54 55 // Verify the length of the byte array 56 Assert.Equal(5, record.GetBytes(2, 0, bb2, 0, 5)); 57 58 Assert.Equal(5, record.GetBytes(2, 0, null, 0, 0)); 59 60 byte[] expected = new byte[] { 1, 2, 7, 8, 9 }; 61 Assert.Equal<byte>(expected, bb2); 62 63 char[] cb1 = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g' }; 64 char[] cb2 = new char[5]; 65 record.SetChars(3, 0, cb1, 0, 3); 66 record.SetChars(3, 2, cb1, 4, 3); 67 68 char[] expectedValue = new char[] { 'a', 'b', 'e', 'f', 'g' }; 69 Assert.Equal(expectedValue.Length, record.GetChars(3, 0, cb2, 0, 5)); 70 Assert.Equal<char>(expectedValue, new string(cb2, 0, (int)record.GetChars(3, 0, null, 0, 0))); 71 72 record.SetString(3, ""); 73 string xyz = "xyz"; 74 record.SetString(3, "xyz"); 75 Assert.Equal(xyz, record.GetString(3)); 76 Assert.Equal(xyz.Length, record.GetChars(3, 0, cb2, 0, 5)); 77 Assert.Equal(xyz, new string(cb2, 0, (int)record.GetChars(3, 0, null, 0, 0))); 78 79 record.SetChars(3, 2, cb1, 4, 3); 80 Assert.Equal(5, record.GetChars(3, 0, cb2, 0, 5)); 81 82 string interleavedResult = "xyefg"; 83 Assert.Equal(interleavedResult, new string(cb2, 0, (int)record.GetChars(3, 0, null, 0, 0))); 84 Assert.Equal(interleavedResult, record.GetString(3)); 85 86 record.SetSqlDateTime(4, SqlDateTime.MaxValue); 87 Assert.Equal(SqlDateTime.MaxValue, record.GetSqlDateTime(4)); 88 89 record.SetSqlDouble(5, SqlDouble.MaxValue); 90 Assert.Equal(SqlDouble.MaxValue, record.GetSqlDouble(5)); 91 92 SqlGuid guid = new SqlGuid("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4"); 93 record.SetSqlGuid(6, guid); 94 Assert.Equal(guid, record.GetSqlGuid(6)); 95 96 record.SetSqlInt16(7, SqlInt16.MaxValue); 97 Assert.Equal(SqlInt16.MaxValue, record.GetSqlInt16(7)); 98 99 record.SetSqlInt32(8, SqlInt32.MaxValue); 100 Assert.Equal(SqlInt32.MaxValue, record.GetSqlInt32(8)); 101 102 record.SetSqlInt64(9, SqlInt64.MaxValue); 103 Assert.Equal(SqlInt64.MaxValue, record.GetSqlInt64(9)); 104 105 record.SetSqlSingle(10, SqlSingle.MinValue); 106 Assert.Equal(SqlSingle.MinValue, record.GetSqlSingle(10)); 107 108 record.SetSqlDecimal(11, SqlDecimal.Null); 109 record.SetSqlDecimal(11, SqlDecimal.MaxValue); 110 Assert.Equal(SqlDecimal.MaxValue, record.GetSqlDecimal(11)); 111 112 record.SetSqlMoney(12, SqlMoney.MaxValue); 113 Assert.Equal(SqlMoney.MaxValue, record.GetSqlMoney(12)); 114 115 116 // Try adding different values to SqlVariant type 117 for (int i = 0; i < record.FieldCount - 1; ++i) 118 { 119 object valueToSet = record.GetSqlValue(i); 120 record.SetValue(record.FieldCount - 1, valueToSet); 121 object o = record.GetSqlValue(record.FieldCount - 1); 122 123 if (o is SqlBinary) 124 { 125 Assert.Equal<byte>(((SqlBinary)valueToSet).Value, ((SqlBinary)o).Value); 126 } 127 else 128 { 129 Assert.Equal(valueToSet, o); 130 } 131 132 record.SetDBNull(record.FieldCount - 1); 133 Assert.Equal(DBNull.Value, record.GetSqlValue(record.FieldCount - 1)); 134 135 record.SetDBNull(i); 136 Assert.Equal(DBNull.Value, record.GetValue(i)); 137 } 138 } 139 140 } 141 } 142