Skip to content

[Breaking change]: SqlDataReader.GetString() behavior change on null #32833

Open
@jhudsoncedaron

Description

@jhudsoncedaron

Description

When upgrading from .NET 6 to .NET 7 we encountered a breaking change in SqlDataReader.

For some reason, upgrading runtime from 6 to 7 changes the behavior of System.Data.SqlDataReader.GetString() to throw when asked to read a null rather than return a null. This is particularly weird because I hadn't gotten around to upgrading SqlDataReader yet. I'm still using the version number from .NET 6.

      IDataReader source;
-    string IDataRow.GetString(int index) => source.GetString(index);
+    // NET 6 behavior: GetString(null column) -> null
+    // NET 7 behavior: GetString(null column) -> throw
+    // BSONReader behavior -> null
+    // Thus expected itnerface behavior is null so we do that
+    string IDataRow.GetString(int index) => source.IsDBNull(index) ? null : source.GetString(index);

Version

.NET 7

Previous behavior

GetString(null column id) => null

New behavior

GetString(null column id) => throw

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load/execute or different run-time behavior.
  • Source incompatible: Source code may encounter a breaking change in behavior when targeting the new runtime/component/SDK, such as compile errors or different run-time behavior.

Reason for change

unknown

Recommended action

Document change

Feature area

Other (please put exact area in description textbox)

Affected APIs

System.Data.SqlClient.GetString()

Metadata

Metadata

Assignees

No one assigned

    Labels

    🏁 Release: .NET 7Work items for the .NET 7 releasebinary incompatibleExisting binaries may encounter a breaking change in behavior.breaking-changeIndicates a .NET Core breaking changesource incompatibleSource code may encounter a breaking change in behavior when targeting the new version.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions