列表中查询的空值

Null Values from query in List

我有一个接收一个列表的 Web 服务,在某些情况下某些列可以为空,当我尝试在我的列表中插入空值时出现错误。 "Column is null"。如果某些列为空,我如何在列表中插入 NULL 值?

  dr = cmd.ExecuteReader();
  List<ClientData> myList = new List<ClientData>();
  while (dr.Read())
   {
     ClientData client = new ClientData();
     client.clientId = dr.GetString(0);
     client.ClientName = dr.GetString(1); **---> NULL VALUE**

使用DbDataReader.IsDBNull

while (dr.Read())
{
 ClientData client = new ClientData();
 client.clientId = dr.GetString(0);
 if(dr.IsDbNull(1))
     client.ClientName = null; 
 else
     client.ClientName = dr.GetString(1); 

可以在调用转换方法前判断是否为null:

client.ClientName = dr.IsDBNull(1) ? null : dr.GetString(1);