Fix SqlDataReader.IsDBNull() for json (#2830)
* fix SqlDataReader.IsDBNull() for json * add testcase * resolve PR commits
This commit is contained in:
Родитель
b8a03e5a3d
Коммит
f5994c80ec
|
@ -5825,6 +5825,10 @@ namespace Microsoft.Data.SqlClient
|
|||
}
|
||||
break;
|
||||
|
||||
case SqlDbTypeExtensions.Json:
|
||||
nullVal.SetToNullOfType(SqlBuffer.StorageType.Json);
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.Fail("unknown null sqlType!" + md.type.ToString());
|
||||
break;
|
||||
|
|
|
@ -6670,6 +6670,10 @@ namespace Microsoft.Data.SqlClient
|
|||
}
|
||||
break;
|
||||
|
||||
case SqlDbTypeExtensions.Json:
|
||||
nullVal.SetToNullOfType(SqlBuffer.StorageType.Json);
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.Fail("unknown null sqlType!" + md.type.ToString());
|
||||
break;
|
||||
|
|
|
@ -61,6 +61,16 @@ namespace Microsoft.Data.SqlClient.ManualTesting.Tests
|
|||
}
|
||||
}
|
||||
|
||||
private void ValidateNullJson(SqlDataReader reader)
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
bool IsNull = reader.IsDBNull(0);
|
||||
_output.WriteLine(IsNull ? "null" : "not null");
|
||||
Assert.True(IsNull);
|
||||
}
|
||||
}
|
||||
|
||||
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
|
||||
public void TestJsonWrite()
|
||||
{
|
||||
|
@ -286,5 +296,38 @@ namespace Microsoft.Data.SqlClient.ManualTesting.Tests
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))]
|
||||
public void TestNullJson()
|
||||
{
|
||||
string tableName = DataTestUtility.GetUniqueNameForSqlServer("Json_Test");
|
||||
|
||||
string tableCreate = "CREATE TABLE " + tableName + " (Data json)";
|
||||
string tableInsert = "INSERT INTO " + tableName + " VALUES (@jsonData)";
|
||||
string tableRead = "SELECT * FROM " + tableName;
|
||||
|
||||
using SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString);
|
||||
connection.Open();
|
||||
using SqlCommand command = connection.CreateCommand();
|
||||
|
||||
//Create Table
|
||||
command.CommandText = tableCreate;
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
//Insert Null value
|
||||
command.CommandText = tableInsert;
|
||||
var parameter = new SqlParameter("@jsonData", SqlDbTypeExtensions.Json);
|
||||
parameter.Value = DBNull.Value;
|
||||
command.Parameters.Add(parameter);
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
//Query the table
|
||||
command.CommandText = tableRead;
|
||||
var reader = command.ExecuteReader();
|
||||
ValidateNullJson(reader);
|
||||
|
||||
reader.Close();
|
||||
DataTestUtility.DropTable(connection, tableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче