How to parse Nullable from a SqlDataReader

后端 未结 6 1044
情歌与酒
情歌与酒 2021-01-17 17:37

The DateTime.TryParse method takes a DateTime as an argument, not a DateTime? ?

Right now I have the following code:

if(!DateTime.TryParse(reader[\"P         


        
6条回答
  •  甜味超标
    2021-01-17 17:48

    Just a combination of top answer and top comment. Thanks @Dylan-Meador and @LukeH.
    (Ed. Note: For the long tail I think this version will save plenty of human time.)

    int x = reader.GetOrdinal("Placed");
    DateTime? _placed = reader.IsDBNull(x) ? (DateTime?)null : reader.GetDateTime(x);
    

提交回复
热议问题