gpt4 book ai didi

c# - 来自读取器的可为空的整数值

转载 作者:太空狗 更新时间:2023-10-29 17:30:32 25 4
gpt4 key购买 nike

我们如何从 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)
);

最佳答案

像上一行一样转换它

mb.Mem_ResAdd4 = reader["Mem_ResAdd4"] == System.DBNull.Value ? null : (string)reader["Mem_ResAdd4"];
//
mb.Mem_ResPin = reader["Mem_ResPin"]== System.DBNull.Value ? default(int):(int)reader["Mem_ResPin"]

关于c# - 来自读取器的可为空的整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18530627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com