C# ADO.NET: nulls and DbNull — is there more efficient syntax?

后端 未结 6 2072
时光取名叫无心
时光取名叫无心 2020-12-14 00:03

I\'ve got a DateTime? that I\'m trying to insert into a field using a DbParameter. I\'m creating the parameter like so:

DbParameter         


        
6条回答
  •  星月不相逢
    2020-12-14 00:32

    If you are using SQLServer, the System.Data.SqlTypes namespace contains some utility classes that avoid the annoying type casting. For example instead of this:

    var val = (object) "abc" ?? DBNull.Value;
    

    you can write this:

    var val = "abc" ?? SqlString.Null;
    

提交回复
热议问题