请问“如何在使用CommandBehavior.SequentialAccess的情况下判断reader[0]是否为空”

Tan18 2008-04-16 12:03:33
如题 我在读取二进制字段时,使用CommandBehavior.SequentialAccess提高效率
但我的数据库中该二进制字段允许为空,请教有什么方法可以实现?

System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
byte[] buffer;
if (reader.Read())
{
buffer = reader[0] != DBNull.Value ? (byte[])reader[0] : null;
}
reader.Close();
经测试上述代码在reader[0]非空的情况下出错
buffer = (byte[])reader[0];
经测试上述代码在reader[0]为空的情况下出错

谢谢关注
...全文
217 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenhua1934 2009-02-20
  • 打赏
  • 举报
回复
你直接用Sql语句不就可以判断出来,然后在进行处理不就可以了
chenhua1934 2009-02-20
  • 打赏
  • 举报
回复
你直接用Sql语句不就可以判断出来,然后在进行处理不就可以了
mywisest 2008-09-03
  • 打赏
  • 举报
回复
什么办法来着?share一下啊
Tan18 2008-04-16
  • 打赏
  • 举报
回复
首先感谢二位
for mqcan
用IF判断和用三元运算没什么区别,
主要是使用了CommandBehavior.SequentialAccess
reader[0]之后再一次reader[0]就会出错
但我又很想用CommandBehavior.SequentialAccess优化查询

for ericzhangbo1982111
有时我并不知道查询的字段名
另外GetOrdinal与[]效果是一样的。
ericzhangbo1982111 2008-04-16
  • 打赏
  • 举报
回复
如果知道字段的名字可以这样写
System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
byte[] buffer;
if (reader.Read())
{
if(!reader.IsDBNull(reader.GetOrdinal("password")))
buffer = (byte[])reader[0];
}

懒牛科技 2008-04-16
  • 打赏
  • 举报
回复
楼主写的应该没有错,而是,楼主不知道 buffer = reader[0] != DBNull.Value ? (byte[])reader[0] : null;
的运算顺序.
试试!

System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess); 
byte[] buffer;
if (reader.Read())
{
buffer = (reader[0] != DBNull.Value ? (byte[])reader[0] : null);
}

如果实在不行,就用IF判断好了.没有必要非用三元运算!
Tan18 2008-04-16
  • 打赏
  • 举报
回复
想到办法了,但不是很好,结贴。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧