我们如何从 Sql Data Reader 读取整数空值
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read() == true)
{
mb.Id = (int)reader["Id"];
mb.Mem_NA = (string)reader["Mem_NA"];
mb.Mem_ResAdd4 = reader["Mem_ResAdd4"] == System.DBNull.Value ? null : (string)reader["Mem_ResAdd4"];
//
mb.Mem_ResPin = reader["Mem_ResPin"] as int? ?? default(int);
// shows the error "Object cannot be cast from DBNull to other types."
}
mb.Mem_ResPin 无法从阅读器读取
CREATE TABLE [dbo].[Mem_Basic] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Mem_NA] VARCHAR (100) NOT NULL,
[Mem_ResAdd4] VARCHAR (100) NULL,
[Mem_ResPin] INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);