Question:
When one executes a stored proc against MS SQL Server, the recordset comes with some type info. Clients are capable of retrieving that type info, e. g. like this (C#/.NET):Specifically in the .NET client, there is
AllowDBNull
in retrieved column properties, but in the scenario above it’s unreliable – it comes across as true
both for columns that came from nullable fields, and for columns that came from nonnullable fields. Is this a limitation of the .NET client, or a shortcoming of the underlying protocol?Answer:
This is a limitation ofSqlDataAdapter
.The TDS protocol does return this information, as you can see in the specification.
In turn,
SqlDataReader
will return this information via GetSchemaTable
. But it does not make its way to a table filled using a DbDataAdapter
.You can see this with the following code
AllowDBNull
as false
and the second as true
.I would advise you in any case to avoid
SqlDataAdapter
, as it is only actually useful in data-binding UI scenarios. In back-end code, just use a SqlDataReader
, or even better: use an ORM such as Dapper or Entity Framework, which will sort out all of this kind of thing for you.If you have better answer, please add a comment about this, thank you!