Sql Data Reader から整数の null 値を読み取る方法
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)
);