Zero data types in data binding?

I am using Typed Data in my C # application to communicate with a database. My database maintains and resolves null values ​​for many records. However, it seems that trying to access a null value using a dataset results in a Cast exception.

Is it possible to make nullable properties (at least those that can store a null value in a database)? If not, why was it designed this way?

+4
source share
4 answers

Is it possible to make nullable properties

No, the Generator tool does not support this.

If this is not so, is it designed in this way?

The dataset goes back to Fx 1.1, Nable values ​​for Fx 2.0

When Fx2 was released, it was decided not to change the structure of the sample dataset (presumably for cost and time reasons).

The Dataset frame has not been updated since then, I think there is an official expression somewhere at the end of life stage.

+1
source

In this answer, Typed NULL support with DataSet support, they (MSFT) say that the data type set does not support nullable values ​​in dot net 4.0

+1
source

Is it possible for properties to be null (at least those that can store a null value in the database)?

Although you cannot store NULL value types in a typed DataSet , you can force the columns of the data set to take DBNull values ​​( column.AllowDBNull = true; ) and create a couple of helper functions that will perform this translation for you.

  public static T? DBToNullable<T>(object dbValue) where T: struct { if (dbValue == DBNull.Value) return null; else return (T)dbValue; } public static object NullableToDB<T>(T? value) where T: struct { if (value.HasValue) return (object)(T)value; else return DBNull.Value; } 

Then they can be used as follows:

  int? value = .... DataRow row = .... row["MyDataColumn"] = NullableToDB(value); value = DBToNullable<int>(row["MyDataColumn"]); 

This should ease the pain.

If this is not so, is it designed in this way?

It was probably designed in this way for historical reasons that are related to the conceptual difference between the NULL database NULL (value: unknown value) and the C # NULL link (which means: this link has not been assigned yet). Of course, these values ​​have changed since how types of null values ​​were added in C #, but by then the DataSet had already DataSet .

+1
source

In .NET, you can create Nullable value types. Can you just use Nullable generic or just put? so that the value type is zero. I'm not sure if a designer automatically creates them for a typed dataset. Take a look at http://xtremebytes.blogspot.com/2010/12/nullable.html for reference

0
source

Source: https://habr.com/ru/post/1334703/


All Articles