From fe38d260daea80f9412bd984147c96c6746735cf Mon Sep 17 00:00:00 2001 From: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com> Date: Mon, 10 Jan 2022 15:07:24 -0800 Subject: [PATCH] fix (#1453) --- .../ProviderAgnostic/ReaderTest/ReaderTest.cs | 132 +++++++++--------- 1 file changed, 67 insertions(+), 65 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/ProviderAgnostic/ReaderTest/ReaderTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/ProviderAgnostic/ReaderTest/ReaderTest.cs index 86e80077ce..697e57cb01 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/ProviderAgnostic/ReaderTest/ReaderTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/ProviderAgnostic/ReaderTest/ReaderTest.cs @@ -276,84 +276,86 @@ public static void SqlDataReader_SqlBuffer_GetFieldValue() { string tableName = DataTestUtility.GetUniqueNameForSqlServer("SqlBuffer_GetFieldValue"); DateTimeOffset dtoffset = DateTimeOffset.Now; - DateTime dateTime = DateTime.Now; + DateTime dt = DateTime.Now; + //Exclude the millisecond because of rounding at some points by SQL Server. + DateTime dateTime = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second); //Arrange DbProviderFactory provider = SqlClientFactory.Instance; - using (DbConnection con = provider.CreateConnection()) - { - con.ConnectionString = DataTestUtility.TCPConnectionString; - con.Open(); - string sqlQueryOne = "CREATE TABLE " + tableName + " ([CustomerId] [int],[FirstName] [nvarchar](50),[BoolCol] [BIT],[ShortCol] [SMALLINT],[ByteCol] [TINYINT],[LongCol] [BIGINT]);"; - string sqlQueryTwo = "ALTER TABLE " + tableName + " ADD [DoubleCol] [FLOAT],[SingleCol] [REAL],[GUIDCol] [uniqueidentifier],[DateTimeCol] [DateTime],[DecimalCol] [SmallMoney],[DateTimeOffsetCol] [DateTimeOffset];"; + using DbConnection con = provider.CreateConnection(); + con.ConnectionString = DataTestUtility.TCPConnectionString; + con.Open(); + string sqlQueryOne = $"CREATE TABLE {tableName} ([CustomerId] [int],[FirstName] [nvarchar](50),[BoolCol] [BIT],[ShortCol] [SMALLINT],[ByteCol] [TINYINT],[LongCol] [BIGINT]);"; + string sqlQueryTwo = $"ALTER TABLE {tableName} ADD [DoubleCol] [FLOAT],[SingleCol] [REAL],[GUIDCol] [uniqueidentifier],[DateTimeCol] [DateTime],[DecimalCol] [SmallMoney],[DateTimeOffsetCol] [DateTimeOffset];"; - try + try + { + using (DbCommand command = provider.CreateCommand()) { - using (DbCommand command = provider.CreateCommand()) - { - command.Connection = con; - command.CommandText = sqlQueryOne; - command.ExecuteNonQuery(); - } - using (DbCommand command = provider.CreateCommand()) - { - command.Connection = con; - command.CommandText = sqlQueryTwo; - command.ExecuteNonQuery(); - } + command.Connection = con; + command.CommandText = sqlQueryOne; + command.ExecuteNonQuery(); + } + using (DbCommand command = provider.CreateCommand()) + { + command.Connection = con; + command.CommandText = sqlQueryTwo; + command.ExecuteNonQuery(); + } - System.Data.SqlTypes.SqlGuid sqlguid = new System.Data.SqlTypes.SqlGuid(Guid.NewGuid()); + System.Data.SqlTypes.SqlGuid sqlguid = new System.Data.SqlTypes.SqlGuid(Guid.NewGuid()); - using (SqlCommand sqlCommand = new SqlCommand("", con as SqlConnection)) - { - sqlCommand.CommandText = "INSERT INTO " + tableName + " VALUES (@CustomerId,@FirstName,@BoolCol,@ShortCol,@ByteCol,@LongCol,@DoubleCol,@SingleCol,@GUIDCol,@DateTimeCol,@DecimalCol,@DateTimeOffsetCol)"; - sqlCommand.Parameters.AddWithValue(@"CustomerId", 1); - sqlCommand.Parameters.AddWithValue(@"FirstName", "Microsoft"); - sqlCommand.Parameters.AddWithValue(@"BoolCol", true); - sqlCommand.Parameters.AddWithValue(@"ShortCol", 3274); - sqlCommand.Parameters.AddWithValue(@"ByteCol", 253); - sqlCommand.Parameters.AddWithValue(@"LongCol", 922222222222); - sqlCommand.Parameters.AddWithValue(@"DoubleCol", 10.7); - sqlCommand.Parameters.AddWithValue(@"SingleCol", 123.546f); - sqlCommand.Parameters.AddWithValue(@"GUIDCol", sqlguid); - sqlCommand.Parameters.AddWithValue(@"DateTimeCol", dateTime); - sqlCommand.Parameters.AddWithValue(@"DecimalCol", 280); - sqlCommand.Parameters.AddWithValue(@"DateTimeOffsetCol", dtoffset); - sqlCommand.ExecuteNonQuery(); - } - using (SqlCommand sqlCommand = new SqlCommand("", con as SqlConnection)) - { - sqlCommand.CommandText = "select top 1 * from " + tableName; - using (DbDataReader reader = sqlCommand.ExecuteReader()) - { - Assert.True(reader.Read()); - Assert.Equal(1, reader.GetFieldValue(0)); - Assert.Equal("Microsoft", reader.GetFieldValue(1)); - Assert.True(reader.GetFieldValue(2)); - Assert.Equal(3274, reader.GetFieldValue(3)); - Assert.Equal(253, reader.GetFieldValue(4)); - Assert.Equal(922222222222, reader.GetFieldValue(5)); - Assert.Equal(10.7, reader.GetFieldValue(6)); - Assert.Equal(123.546f, reader.GetFieldValue(7)); - Assert.Equal(sqlguid, reader.GetFieldValue(8)); - Assert.Equal(sqlguid.Value, reader.GetFieldValue(8).Value); - Assert.Equal(dateTime.ToString("dd/MM/yyyy HH:mm:ss"), reader.GetFieldValue(9).ToString("dd/MM/yyyy HH:mm:ss")); - Assert.Equal(280, reader.GetFieldValue(10)); - Assert.Equal(dtoffset, reader.GetFieldValue(11)); - } - } + using (SqlCommand sqlCommand = new SqlCommand("", con as SqlConnection)) + { + sqlCommand.CommandText = $"INSERT INTO {tableName} " + + "VALUES (@CustomerId,@FirstName,@BoolCol,@ShortCol,@ByteCol,@LongCol,@DoubleCol,@SingleCol" + + ",@GUIDCol,@DateTimeCol,@DecimalCol,@DateTimeOffsetCol)"; + sqlCommand.Parameters.AddWithValue(@"CustomerId", 1); + sqlCommand.Parameters.AddWithValue(@"FirstName", "Microsoft"); + sqlCommand.Parameters.AddWithValue(@"BoolCol", true); + sqlCommand.Parameters.AddWithValue(@"ShortCol", 3274); + sqlCommand.Parameters.AddWithValue(@"ByteCol", 253); + sqlCommand.Parameters.AddWithValue(@"LongCol", 922222222222); + sqlCommand.Parameters.AddWithValue(@"DoubleCol", 10.7); + sqlCommand.Parameters.AddWithValue(@"SingleCol", 123.546f); + sqlCommand.Parameters.AddWithValue(@"GUIDCol", sqlguid); + sqlCommand.Parameters.AddWithValue(@"DateTimeCol", dateTime); + sqlCommand.Parameters.AddWithValue(@"DecimalCol", 280); + sqlCommand.Parameters.AddWithValue(@"DateTimeOffsetCol", dtoffset); + sqlCommand.ExecuteNonQuery(); } - finally + using (SqlCommand sqlCommand = new SqlCommand("", con as SqlConnection)) { - //cleanup - using (DbCommand cmd = provider.CreateCommand()) + sqlCommand.CommandText = "select top 1 * from " + tableName; + using (DbDataReader reader = sqlCommand.ExecuteReader()) { - cmd.Connection = con; - cmd.CommandText = "drop table " + tableName; - cmd.ExecuteNonQuery(); + Assert.True(reader.Read()); + Assert.Equal(1, reader.GetFieldValue(0)); + Assert.Equal("Microsoft", reader.GetFieldValue(1)); + Assert.True(reader.GetFieldValue(2)); + Assert.Equal(3274, reader.GetFieldValue(3)); + Assert.Equal(253, reader.GetFieldValue(4)); + Assert.Equal(922222222222, reader.GetFieldValue(5)); + Assert.Equal(10.7, reader.GetFieldValue(6)); + Assert.Equal(123.546f, reader.GetFieldValue(7)); + Assert.Equal(sqlguid, reader.GetFieldValue(8)); + Assert.Equal(sqlguid.Value, reader.GetFieldValue(8).Value); + Assert.Equal(dateTime.ToString("dd/MM/yyyy HH:mm:ss.fff"), reader.GetFieldValue(9).ToString("dd/MM/yyyy HH:mm:ss.fff")); + Assert.Equal(280, reader.GetFieldValue(10)); + Assert.Equal(dtoffset, reader.GetFieldValue(11)); } } } + finally + { + //cleanup + using (DbCommand cmd = provider.CreateCommand()) + { + cmd.Connection = con; + cmd.CommandText = "drop table " + tableName; + cmd.ExecuteNonQuery(); + } + } } } }