我正在使用sql命令和sql reader(如
)读取数据using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
// how can I check is reader["Name"] != null?
myObj.Name = (string)reader["Name"];
}
}
答案 0 :(得分:1)
您可以使用IsDBNull
if(!reader.IsDBNull("Name")){
myObj.Name = (string)reader["Name"];
}
除非你能这样做:
if (reader["Name"] != DBNull.Value)
{
myObj.Name = (string)reader["Name"];
}
OR
myObj.Name = (reader.IsDBNull("Name")? "": sqlreader.GetString(indexofName));