首页 新闻 会员 周边 捐助

数据库字段是null C# 怎么datareader读取 怎么让它读出来是让它显示到页面是0

0
悬赏园豆:5 [已解决问题] 解决于 2013-01-30 11:54

数据库字段是null C# 怎么datareader读取 怎么让它读出来是让它显示到页面是0

 

那位大侠帮忙下

问题补充:

能给个实例吗?我是用datareader 读取的 

 


            using (SqlDataReader reader = DBUtility.SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, "dbo.P_GetUserBuyDetail", parameters))
            {
           
                if (reader.Read())
                {
                      Models.Userinfo user = new Models.Userinfo(
                          reader.GetString(0),
                          reader.GetString(2),
                          reader.GetString(3),
                          reader.GetInt32(4),
                          reader.GetDecimal(5),
                          reader.GetInt32(6),
                          reader.GetDecimal(8),
                          reader.GetDecimal(9),
                          reader.GetDecimal(11),
                          reader.GetDateTime(13),
                          reader.GetByte(16),
                          reader.GetBoolean(17)
                          );
                    list.Add(user);
                }
           
            }
            return list;

第6个下表为空 我怎么去判断

 

我用的是存储过程读的啊

yujinchao的主页 yujinchao | 菜鸟二级 | 园豆:248
提问于:2013-01-29 16:45
< >
分享
最佳答案
0

reader.IsDBNull(6) ? 0 : reader.GetInt32(6),

收获园豆:2
Launcher | 高人七级 |园豆:45050 | 2013-01-29 17:28
其他回答(5)
0

判断

柳轩涤俗 | 园豆:202 (菜鸟二级) | 2013-01-29 16:50

能给个实例吗?我是用datareader 读取的 

 


            using (SqlDataReader reader = DBUtility.SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, "dbo.P_GetUserBuyDetail", parameters))
            {
           
                if (reader.Read())
                {
                      Models.Userinfo user = new Models.Userinfo(
                          reader.GetString(0),
                          reader.GetString(2),
                          reader.GetString(3),
                          reader.GetInt32(4),
                          reader.GetDecimal(5),
                          reader.GetInt32(6),
                          reader.GetDecimal(8),
                          reader.GetDecimal(9),
                          reader.GetDecimal(11),
                          reader.GetDateTime(13),
                          reader.GetByte(16),
                          reader.GetBoolean(17)
                          );
                    list.Add(user);
                }
           
            }
            return list;

第6个下表为空 我怎么去判断

支持(0) 反对(0) yujinchao | 园豆:248 (菜鸟二级) | 2013-01-29 17:04
0

select isnull(字段,0) from 表

Rich.T | 园豆:3440 (老鸟四级) | 2013-01-29 16:56

程序怎么写我用的是存储过程写的

支持(0) 反对(0) yujinchao | 园豆:248 (菜鸟二级) | 2013-01-29 17:08
0

读取的时候结合Convert.isDbull()和3元运算符。

eg:

字符串:Convert.isDbull(reader.GetInt32(0))?"":reader.GetInt32(0);

数值:Convert.isDbull(reader.GetInt32(4))?0:reader.GetInt32(4);

收获园豆:1
滴答的雨 | 园豆:3660 (老鸟四级) | 2013-01-29 17:20
0

isnull(字段1,0)

写在sql中

收获园豆:1
chenping2008 | 园豆:9836 (大侠五级) | 2013-01-30 09:40
0

用三元表达式:reader.IsDBNull(6)?0:reader.GetInt32(6);

收获园豆:1
轻狂の书生 | 园豆:1042 (小虾三级) | 2013-01-30 09:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册