|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
| 5 | +using System; |
5 | 6 | using System.Collections.Generic; |
6 | 7 | using System.Data; |
7 | 8 | using System.Text; |
@@ -225,5 +226,81 @@ private static bool IsColumnBitSet(SqlConnection con, string selectQuery, int in |
225 | 226 | } |
226 | 227 | return columnSetPresent; |
227 | 228 | } |
| 229 | + |
| 230 | + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] |
| 231 | + public static void CheckHiddenColumns() |
| 232 | + { |
| 233 | + // hidden columns can be found by using CommandBehavious.KeyInfo or at the sql level |
| 234 | + // by using the FOR BROWSE option. These features return the column information requested and |
| 235 | + // also include any key information required to be able to find the row containing the data |
| 236 | + // requested. The additional key infomration is provided as hidden columns and can be seen using |
| 237 | + // the difference between VisibleFieldCount and FieldCount on the reader |
| 238 | + |
| 239 | + string tempTableName = DataTestUtility.GenerateObjectName(); |
| 240 | + |
| 241 | + string createQuery = $@" |
| 242 | +create table [{tempTableName}] ( |
| 243 | + user_id int not null identity(1,1), |
| 244 | + first_name varchar(100) null, |
| 245 | + last_name varchar(100) null); |
| 246 | +
|
| 247 | +alter table [{tempTableName}] add constraint pk_{tempTableName}_user_id primary key (user_id); |
| 248 | +
|
| 249 | +insert into [{tempTableName}] (first_name,last_name) values ('Joe','Smith') |
| 250 | +"; |
| 251 | + |
| 252 | + string dataQuery = $@"select first_name, last_name from [{tempTableName}]"; |
| 253 | + |
| 254 | + |
| 255 | + int fieldCount = 0; |
| 256 | + int visibleFieldCount = 0; |
| 257 | + Type[] types = null; |
| 258 | + string[] names = null; |
| 259 | + |
| 260 | + using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString)) |
| 261 | + { |
| 262 | + connection.Open(); |
| 263 | + |
| 264 | + try |
| 265 | + { |
| 266 | + using (SqlCommand createCommand = new SqlCommand(createQuery, connection)) |
| 267 | + { |
| 268 | + createCommand.ExecuteNonQuery(); |
| 269 | + } |
| 270 | + |
| 271 | + using (SqlCommand queryCommand = new SqlCommand(dataQuery, connection)) |
| 272 | + { |
| 273 | + using (SqlDataReader reader = queryCommand.ExecuteReader(CommandBehavior.KeyInfo)) |
| 274 | + { |
| 275 | + fieldCount = reader.FieldCount; |
| 276 | + visibleFieldCount = reader.VisibleFieldCount; |
| 277 | + types = new Type[fieldCount]; |
| 278 | + names = new string[fieldCount]; |
| 279 | + for (int index = 0; index < fieldCount; index++) |
| 280 | + { |
| 281 | + types[index] = reader.GetFieldType(index); |
| 282 | + names[index] = reader.GetName(index); |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + } |
| 287 | + finally |
| 288 | + { |
| 289 | + DataTestUtility.DropTable(connection, tempTableName); |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + Assert.Equal(3, fieldCount); |
| 294 | + Assert.Equal(2, visibleFieldCount); |
| 295 | + Assert.NotNull(types); |
| 296 | + Assert.NotNull(names); |
| 297 | + |
| 298 | + // requested fields |
| 299 | + Assert.Contains("first_name", names, StringComparer.Ordinal); |
| 300 | + Assert.Contains("last_name", names, StringComparer.Ordinal); |
| 301 | + |
| 302 | + // hidden field |
| 303 | + Assert.Contains("user_id", names, StringComparer.Ordinal); |
| 304 | + } |
228 | 305 | } |
229 | 306 | } |
0 commit comments