.NET types with null value and null database values

If .NET 2.0 with null types has existed since version 1, then DBNull.Value is not needed in the first place?

Or is RDBMS null not related to .NET null? This DBNull.Value will still be needed, one way or another, regardless of whether .NET version 1 already has types with a null value.

+3
source share
4 answers

If System.Data.DataSetExtensions.dll , everything should pass, I assume that DBNull.Value probably will not exist if NULL value types were available at that time.

DataRow "foo", DBNull.Value, ...

row.Field<int?>("foo");  // returns a nullable int set to null
row.Field<int>("foo");   // throws an InvalidCastException

, , #? . , IDataReader/IDataRecord.

+3

Nullable types .NET , , null. DBNull - " , ". "" , , null , , NULL DBNull .

+7

null DBNull.Value - .NET.

:

public string CustomerName(int Id)
{
   SqlCommand cmd = 
         new SqlCommand("SELECT Name FROM Customer WHERE id = " + custId, conn);

   object result = cmd.ExecuteScalar();

   if (result == null)
      return "Customer not found";

   if (result == System.DBNull.Value)
      return "Customer found but name is null";

   return (string) result;
}

.

+6

.

Nullable .NET , , . DBNull - " , ". null - "" null, - null , NULL DBNull .

, , . , , null DBNull.

, null , null == null - true. null , . null == null ; null , , . , DBNull , DBNull.Value == DBNull.Value false.

, DBNull , . DBNull.Value == DBNull.Value true, false .

+1

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


All Articles