vb.net 避免在每列上检查 DataRow.IsDBNull 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14401264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
avoid checking for DataRow.IsDBNull on each column?
提问by D Stanley
My code is 2x longer than it would be if I could automatically set IsDBNull
to ""
or simply roll over it without an error.
我的代码是2倍长于这将是,如果我可以自动设置IsDBNull
到""
或只是翻转它没有一个错误。
This is my code:
这是我的代码:
Dim conn As New SqlConnection
conn.ConnectionString = Module1.DBConn2
Dim sqlCommand = New SqlCommand("SELECT * FROM table", conn)
conn.Open()
Dim sqlDataset As DataSet = New DataSet()
Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
sqlDataAdapter.Fill(sqlDataset)
conn.Close()
For Each rs As DataRow In sqlDataset.Tables(0).Rows
If Not IsDBNull(rs("column")) Then
Response.Write(rs("column"))
Else
Response.Write("")
End If
Response.Write("some stuff to write")
If Not IsDBNull(rs("column2")) Then
Response.Write(rs("column2"))
Else
Response.Write("")
End If
Next
In that case I'd just like to type Response.Write(rs("column"))
instead of the If
statement, and if column
IsDBNull
then output an empty string.
在这种情况下,我只想输入Response.Write(rs("column"))
而不是If
语句,column
IsDBNull
然后输出一个空字符串。
How can I do this?
我怎样才能做到这一点?
Many thanks in advance!
提前谢谢了!
回答by Tim Schmelter
You could simply use String.Join
and pass row.ItemArray
:
您可以简单地使用String.Join
和传递row.ItemArray
:
For Each row As DataRow In sqlDataset.Tables(0).Rows
Response.Write(String.Join("", row.ItemArray))
Next
That works since DBNull.ToString
returns an empty string.
这是有效的,因为DBNull.ToString
返回一个空字符串。
If you want to address every column, you can use the strongly typed DataRowExtensions.Field
method which supports nullables and return null
/Nothing
for string. Then you could use the null-coalescing operator
(??
in C#, If
in VB).
如果您想处理每一列,您可以使用DataRowExtensions.Field
支持可空值的强类型方法并为字符串返回null
/ Nothing
。然后你可以使用null-coalescing operator
(??
在 C# 中,If
在 VB 中)。
Dim rowInfo = String.Format("{0}{1}{2}",
If(row.Field(Of String)("Column1"), ""),
If(row.Field(Of String)("Column2"), ""),
If(row.Field(Of String)("Column3"), ""))
However, note that String.Format
will convert null
/Nothing
to ""
implicitely anyway, so the If
is redundant and just fyi.
但是,请注意,无论如何String.Format
都会将null
/转换Nothing
为""
隐式,因此If
是多余的,只是fyi。
MSDN:
微软:
If the object specified by index is a null reference (Nothing in Visual Basic), then the format item is replaced by the empty string ("").
如果 index 指定的对象是空引用(在 Visual Basic 中为 Nothing),则格式项将替换为空字符串 ("")。
回答by D Stanley
Here's a one-liner:
这是一个单行:
Response.Write(rs.IsNull("column") ? "" : rs("column"));
or make it an extension method:
或使其成为扩展方法:
public string GetValueOrBlankString(this DataRow rs, string column)
{
return rs.IsNull(column) ? "" : rs(column).ToString();
}
then call it as:
然后将其称为:
Response.Write(rs.GetValueOrBlankString("column"));
回答by Ceres
Dataset Extensions give you a clean way of doing and it's also strongly typed. The type must match the column type in the database though. If the database column can be null, then use a nullable type like below. The null values become Nothing for the returned nullable type.
数据集扩展为您提供了一种干净的方式,并且它也是强类型的。但是,该类型必须与数据库中的列类型匹配。如果数据库列可以为空,则使用如下所示的可为空类型。对于返回的可为空类型,空值变为 Nothing。
For Each rs As DataRow In sqlDataset.Tables(0).Rows
'If string, you can use this. Null becomes nothing for the string.
Response.Write(rs.field(of String)("column"))
'if it's another type
Response.Write(rs.field(of Integer?)("column"))
Next
回答by gbinflames
Ceres's answer is probably the best given that it avoids any sort of null testing, but it's worth noting that the 'IIF' function would also work pretty well her. It's still going to do the test for null but it's much more compact than how Joe was originally doing it. Something like this should do the trick:
Ceres 的答案可能是最好的,因为它避免了任何类型的空测试,但值得注意的是,“IIF”功能也可以很好地工作。它仍然会为 null 进行测试,但它比 Joe 最初所做的要紧凑得多。像这样的事情应该可以解决问题:
For Each rs As DataRow In sqlDataset.Tables(0).Rows
Response.Write( IIF( IsDBNull(rs("column")), "", rs("column") ) )
Next
What's neat with this is you can substitute the "" for whatever you want to output if the value is in fact null ( a nice little added bonus. )
这样做的好处是,如果值实际上为 null,您可以将“”替换为您想要输出的任何内容(一个不错的额外奖励。)
Here's some info on the 'IIF' function for those who don't know what it is:
以下是有关“IIF”功能的一些信息,供不知道它是什么的人使用:
http://msdn.microsoft.com/en-ca/library/27ydhh0d(v=vs.71).aspx
http://msdn.microsoft.com/en-ca/library/27ydhh0d(v=vs.71).aspx