Skip to content

Commit fe38d26

Browse files
fix (#1453)
1 parent 5a53a36 commit fe38d26

File tree

1 file changed

+67
-65
lines changed
  • src/Microsoft.Data.SqlClient/tests/ManualTests/ProviderAgnostic/ReaderTest

1 file changed

+67
-65
lines changed

src/Microsoft.Data.SqlClient/tests/ManualTests/ProviderAgnostic/ReaderTest/ReaderTest.cs

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -276,84 +276,86 @@ public static void SqlDataReader_SqlBuffer_GetFieldValue()
276276
{
277277
string tableName = DataTestUtility.GetUniqueNameForSqlServer("SqlBuffer_GetFieldValue");
278278
DateTimeOffset dtoffset = DateTimeOffset.Now;
279-
DateTime dateTime = DateTime.Now;
279+
DateTime dt = DateTime.Now;
280+
//Exclude the millisecond because of rounding at some points by SQL Server.
281+
DateTime dateTime = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
280282
//Arrange
281283
DbProviderFactory provider = SqlClientFactory.Instance;
282284

283-
using (DbConnection con = provider.CreateConnection())
284-
{
285-
con.ConnectionString = DataTestUtility.TCPConnectionString;
286-
con.Open();
287-
string sqlQueryOne = "CREATE TABLE " + tableName + " ([CustomerId] [int],[FirstName] [nvarchar](50),[BoolCol] [BIT],[ShortCol] [SMALLINT],[ByteCol] [TINYINT],[LongCol] [BIGINT]);";
288-
string sqlQueryTwo = "ALTER TABLE " + tableName + " ADD [DoubleCol] [FLOAT],[SingleCol] [REAL],[GUIDCol] [uniqueidentifier],[DateTimeCol] [DateTime],[DecimalCol] [SmallMoney],[DateTimeOffsetCol] [DateTimeOffset];";
285+
using DbConnection con = provider.CreateConnection();
286+
con.ConnectionString = DataTestUtility.TCPConnectionString;
287+
con.Open();
288+
string sqlQueryOne = $"CREATE TABLE {tableName} ([CustomerId] [int],[FirstName] [nvarchar](50),[BoolCol] [BIT],[ShortCol] [SMALLINT],[ByteCol] [TINYINT],[LongCol] [BIGINT]);";
289+
string sqlQueryTwo = $"ALTER TABLE {tableName} ADD [DoubleCol] [FLOAT],[SingleCol] [REAL],[GUIDCol] [uniqueidentifier],[DateTimeCol] [DateTime],[DecimalCol] [SmallMoney],[DateTimeOffsetCol] [DateTimeOffset];";
289290

290-
try
291+
try
292+
{
293+
using (DbCommand command = provider.CreateCommand())
291294
{
292-
using (DbCommand command = provider.CreateCommand())
293-
{
294-
command.Connection = con;
295-
command.CommandText = sqlQueryOne;
296-
command.ExecuteNonQuery();
297-
}
298-
using (DbCommand command = provider.CreateCommand())
299-
{
300-
command.Connection = con;
301-
command.CommandText = sqlQueryTwo;
302-
command.ExecuteNonQuery();
303-
}
295+
command.Connection = con;
296+
command.CommandText = sqlQueryOne;
297+
command.ExecuteNonQuery();
298+
}
299+
using (DbCommand command = provider.CreateCommand())
300+
{
301+
command.Connection = con;
302+
command.CommandText = sqlQueryTwo;
303+
command.ExecuteNonQuery();
304+
}
304305

305-
System.Data.SqlTypes.SqlGuid sqlguid = new System.Data.SqlTypes.SqlGuid(Guid.NewGuid());
306+
System.Data.SqlTypes.SqlGuid sqlguid = new System.Data.SqlTypes.SqlGuid(Guid.NewGuid());
306307

307-
using (SqlCommand sqlCommand = new SqlCommand("", con as SqlConnection))
308-
{
309-
sqlCommand.CommandText = "INSERT INTO " + tableName + " VALUES (@CustomerId,@FirstName,@BoolCol,@ShortCol,@ByteCol,@LongCol,@DoubleCol,@SingleCol,@GUIDCol,@DateTimeCol,@DecimalCol,@DateTimeOffsetCol)";
310-
sqlCommand.Parameters.AddWithValue(@"CustomerId", 1);
311-
sqlCommand.Parameters.AddWithValue(@"FirstName", "Microsoft");
312-
sqlCommand.Parameters.AddWithValue(@"BoolCol", true);
313-
sqlCommand.Parameters.AddWithValue(@"ShortCol", 3274);
314-
sqlCommand.Parameters.AddWithValue(@"ByteCol", 253);
315-
sqlCommand.Parameters.AddWithValue(@"LongCol", 922222222222);
316-
sqlCommand.Parameters.AddWithValue(@"DoubleCol", 10.7);
317-
sqlCommand.Parameters.AddWithValue(@"SingleCol", 123.546f);
318-
sqlCommand.Parameters.AddWithValue(@"GUIDCol", sqlguid);
319-
sqlCommand.Parameters.AddWithValue(@"DateTimeCol", dateTime);
320-
sqlCommand.Parameters.AddWithValue(@"DecimalCol", 280);
321-
sqlCommand.Parameters.AddWithValue(@"DateTimeOffsetCol", dtoffset);
322-
sqlCommand.ExecuteNonQuery();
323-
}
324-
using (SqlCommand sqlCommand = new SqlCommand("", con as SqlConnection))
325-
{
326-
sqlCommand.CommandText = "select top 1 * from " + tableName;
327-
using (DbDataReader reader = sqlCommand.ExecuteReader())
328-
{
329-
Assert.True(reader.Read());
330-
Assert.Equal(1, reader.GetFieldValue<int>(0));
331-
Assert.Equal("Microsoft", reader.GetFieldValue<string>(1));
332-
Assert.True(reader.GetFieldValue<bool>(2));
333-
Assert.Equal(3274, reader.GetFieldValue<short>(3));
334-
Assert.Equal(253, reader.GetFieldValue<byte>(4));
335-
Assert.Equal(922222222222, reader.GetFieldValue<long>(5));
336-
Assert.Equal(10.7, reader.GetFieldValue<double>(6));
337-
Assert.Equal(123.546f, reader.GetFieldValue<float>(7));
338-
Assert.Equal(sqlguid, reader.GetFieldValue<Guid>(8));
339-
Assert.Equal(sqlguid.Value, reader.GetFieldValue<System.Data.SqlTypes.SqlGuid>(8).Value);
340-
Assert.Equal(dateTime.ToString("dd/MM/yyyy HH:mm:ss"), reader.GetFieldValue<DateTime>(9).ToString("dd/MM/yyyy HH:mm:ss"));
341-
Assert.Equal(280, reader.GetFieldValue<decimal>(10));
342-
Assert.Equal(dtoffset, reader.GetFieldValue<DateTimeOffset>(11));
343-
}
344-
}
308+
using (SqlCommand sqlCommand = new SqlCommand("", con as SqlConnection))
309+
{
310+
sqlCommand.CommandText = $"INSERT INTO {tableName} "
311+
+ "VALUES (@CustomerId,@FirstName,@BoolCol,@ShortCol,@ByteCol,@LongCol,@DoubleCol,@SingleCol"
312+
+ ",@GUIDCol,@DateTimeCol,@DecimalCol,@DateTimeOffsetCol)";
313+
sqlCommand.Parameters.AddWithValue(@"CustomerId", 1);
314+
sqlCommand.Parameters.AddWithValue(@"FirstName", "Microsoft");
315+
sqlCommand.Parameters.AddWithValue(@"BoolCol", true);
316+
sqlCommand.Parameters.AddWithValue(@"ShortCol", 3274);
317+
sqlCommand.Parameters.AddWithValue(@"ByteCol", 253);
318+
sqlCommand.Parameters.AddWithValue(@"LongCol", 922222222222);
319+
sqlCommand.Parameters.AddWithValue(@"DoubleCol", 10.7);
320+
sqlCommand.Parameters.AddWithValue(@"SingleCol", 123.546f);
321+
sqlCommand.Parameters.AddWithValue(@"GUIDCol", sqlguid);
322+
sqlCommand.Parameters.AddWithValue(@"DateTimeCol", dateTime);
323+
sqlCommand.Parameters.AddWithValue(@"DecimalCol", 280);
324+
sqlCommand.Parameters.AddWithValue(@"DateTimeOffsetCol", dtoffset);
325+
sqlCommand.ExecuteNonQuery();
345326
}
346-
finally
327+
using (SqlCommand sqlCommand = new SqlCommand("", con as SqlConnection))
347328
{
348-
//cleanup
349-
using (DbCommand cmd = provider.CreateCommand())
329+
sqlCommand.CommandText = "select top 1 * from " + tableName;
330+
using (DbDataReader reader = sqlCommand.ExecuteReader())
350331
{
351-
cmd.Connection = con;
352-
cmd.CommandText = "drop table " + tableName;
353-
cmd.ExecuteNonQuery();
332+
Assert.True(reader.Read());
333+
Assert.Equal(1, reader.GetFieldValue<int>(0));
334+
Assert.Equal("Microsoft", reader.GetFieldValue<string>(1));
335+
Assert.True(reader.GetFieldValue<bool>(2));
336+
Assert.Equal(3274, reader.GetFieldValue<short>(3));
337+
Assert.Equal(253, reader.GetFieldValue<byte>(4));
338+
Assert.Equal(922222222222, reader.GetFieldValue<long>(5));
339+
Assert.Equal(10.7, reader.GetFieldValue<double>(6));
340+
Assert.Equal(123.546f, reader.GetFieldValue<float>(7));
341+
Assert.Equal(sqlguid, reader.GetFieldValue<Guid>(8));
342+
Assert.Equal(sqlguid.Value, reader.GetFieldValue<System.Data.SqlTypes.SqlGuid>(8).Value);
343+
Assert.Equal(dateTime.ToString("dd/MM/yyyy HH:mm:ss.fff"), reader.GetFieldValue<DateTime>(9).ToString("dd/MM/yyyy HH:mm:ss.fff"));
344+
Assert.Equal(280, reader.GetFieldValue<decimal>(10));
345+
Assert.Equal(dtoffset, reader.GetFieldValue<DateTimeOffset>(11));
354346
}
355347
}
356348
}
349+
finally
350+
{
351+
//cleanup
352+
using (DbCommand cmd = provider.CreateCommand())
353+
{
354+
cmd.Connection = con;
355+
cmd.CommandText = "drop table " + tableName;
356+
cmd.ExecuteNonQuery();
357+
}
358+
}
357359
}
358360
}
359361
}

0 commit comments

Comments
 (0)