避免在每列上检查DataRow.IsDBNull? [英] avoid checking for DataRow.IsDBNull on each column?
问题描述
我的代码比我自动将 IsDBNull
设置为或只是滚动
这是我的代码:
Dim conn As New SqlConnection
$ / pre>
conn.ConnectionString = Module1.DBConn2
Dim sqlCommand = New SqlCommand(SELECT * FROM table,conn)
conn.Open()
Dim sqlDataset作为DataSet = New DataSet()
Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
sqlDataAdapter.Fill(sqlDataset)
conn.Close()
对于每个rs As DataRow In sqlDataset.Tables(0).Rows
如果不是IsDBNull(rs(column))然后
Response.Write(rs(column))
Else
Response.Write()
End If
Response.Write(some stuff to write)
如果不是IsDBNull(rs(column2 ))然后
Response.Write(rs(column2))
Else
Response.Write()
结束如果
下一个
在这种情况下,我只想键入
Response.Write(rs(column))
而不是如果
语句,如果列
IsDBNull
然后输出一个空字符串。 / p>
我该怎么做?
非常感谢提前!
解决方案您可以使用
String.Join
并通过row.ItemArray
:对于每行作为DataRow在sqlDataset.Tables(0).Rows
Response.Write(String.Join(,row.ItemArray))
下一个
这是因为
DBNull.ToString
返回一个空字符串。
如果你想处理每一列,您可以使用强类型的
DataRowExtensions.Field
方法,它支持可空值并返回null
/没有
串。然后,您可以使用C#中的null-coalescing运算符
(??
code>在VB)。
Dim rowInfo = String.Format({0} {1} {2},
If Field(Of String)(Column1),),
If(row.Field(Of String)(Column2),),
If(row.Field字符串)(Column3),))
但是,请注意
String.Format
将转换null
/Nothing
to无论如何,所以
如果
是多余的,只是。
MSDN :
如果由索引指定的对象是空引用(
Visual Basic中为Nothing),则格式项替换为空字符串
()。
My code is 2x longer than it would be if I could automatically set
IsDBNull
to""
or simply roll over it without an error.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 theIf
statement, and ifcolumn
IsDBNull
then output an empty string.How can I do this?
Many thanks in advance!
解决方案You could simply use
String.Join
and passrow.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.If you want to address every column, you can use the strongly typed
DataRowExtensions.Field
method which supports nullables and returnnull
/Nothing
for string. Then you could use thenull-coalescing operator
(??
in C#,If
in 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 convertnull
/Nothing
to""
implicitely anyway, so theIf
is redundant and just 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 ("").
这篇关于避免在每列上检查DataRow.IsDBNull?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!