SQL DataReader 如何显示查询中的空值
我有一个 DataReader 和一个 StringBuilder (C#.NET),按以下方式使用;
while (reader.Read())
{
sb.AppendFormat("{0},{1},{2},",reader["Col1"], reader["Col2"], reader["Col3"]);
}
这对我的使用非常有用,但是当一行为空时,我需要它返回“null”,而不仅仅是“”。实现这一目标的好方法是什么?
非常感谢建议
I have a DataReader and a StringBuilder (C#.NET) used in the following way;
while (reader.Read())
{
sb.AppendFormat("{0},{1},{2},",reader["Col1"], reader["Col2"], reader["Col3"]);
}
Which works great for my use, but when a row is null I need it to return "null", instead of just "". What would be a good way of accomplishing that?
Suggestions are very appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
或者,如果您要重复使用它,这可能是范围严格的扩展方法的理想候选者,例如:
因此您可以编写:
如果您需要使用此逻辑,这肯定会让事情变得更整洁在一次
StringBuilder.AppendFormat
调用中多次:Try:
Alternatively, if you're going to be using this repeatedly, this is probably an ideal candidate for a tightly scoped Extension Method, for example:
So you could then write:
Which would definately make things tidier if you needed to use this logic multiple times inside one
StringBuilder.AppendFormat
call: