7

Is there a way I can determine in .NET, for any arbitrary SQL Server result set, if a given column in the result can contain nulls?

For example, if I have the statements

Select NullableColumn From MyTable

and

Select IsNull(NullableColumn, '5') as NotNullColumn From MyTable

and I get a datareader like this:

var cmd = new SqlCommand(statement, connection);
var rdr = cmd.ExecuteReader();

can I have a function like this?

bool ColumnMayHaveNullData(SqlDataReader rdr, int ordinal)
{
   //????
}

I want it to return true for the first statement, and false for the second statement.

rdr.GetSchemaTable() doesn't work for this because it returns whether the underlying column can be null, which is not what I want. There are functions on datareader that return the underlying sql type of the field, but none seem to tell me if it can be null..

2
  • 1
    Can I ask why you want to know? Commented Apr 14, 2011 at 23:26
  • @tomfanning I'm hacking an ORM. I want to log anytime someone tries to run a query where a nullable db field maps to a non-nullable value type. Commented Apr 15, 2011 at 0:22

2 Answers 2

1

Unfortunately You can't because SQL server has no way of determining whether a field is nullable or not. You can do arbitral transformations on fields in result set (operators, function calls etc.) and those transformations do not have metadata about them whether they can or can't return null. So You have to figure that out manually or use views with schemabinding...

Sign up to request clarification or add additional context in comments.

Comments

1

I'm a bit confused:

"doesn't work for this because it returns whether the underlying column can be null, which is not what I want. "

"but none seem to tell me if it can be null.."

You can query the underlying table to see if a column is null-able (assuming that is what you want).

SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTable' AND COLUMN_NAME = 'MyColumn'

4 Comments

i want to know if a column in the result set can contain nulls, not if the column in the underlying table can contain nulls. In my example, I use an IsNull() in the select statement to turn null values into non-null values. In this case, that result set column cannot contain nulls, even though the underlying table column can.
The result set column can still contain nulls, but you've replaced all the nulls with non nulls. So you want to know if the data was 'scrubbed'? You could retrieve the same column twice...one would be raw with nulls and the other would be the scrubbed version of the same column.
If I've replaced all nulls with non nulls, then the result set column doesn't contain nulls. That's what I'm trying to find out, but without having to loop through the rows to look at individual values. Basically I'm trying to validate ad-hoc queries against an object model at runtime. You and I can tell just by looking at a query which result columns may contain nulls, so there should be a way to write a function that can figure that out.
Also in my opinion using isnull IN queries that return null is not the kosher way( though Your milagr may vary) god created nullable types so that the map to nullable result fields and the we call GetValueOrDefault in the property implementation in the poco and everything is clean;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.