2016-11-04 47 views
0

我讀數據使用SQL命令和SQL讀者喜歡檢查NULL使用SQL數據讀取器

using (var reader = command.ExecuteReader()) 
{ 
    if (reader.Read()) 
    { 
     // how can I check is reader["Name"] != null? 
     myObj.Name = (string)reader["Name"]; 
    } 
} 
+1

'if(!reader.IsDBNull [「Name」]){' –

回答

1

可以使用時IsDBNull以便

if(!reader.IsDBNull("Name")){ 
    myObj.Name = (string)reader["Name"]; 
} 

除非你可以做:

if (reader["Name"] != DBNull.Value) 
{ 
    myObj.Name = (string)reader["Name"]; 
} 

myObj.Name = (reader.IsDBNull("Name")? "": sqlreader.GetString(indexofName));