避免在每列上检查DataRow.IsDBNull? [英] avoid checking for DataRow.IsDBNull on each column?

查看:199
本文介绍了避免在每列上检查DataRow.IsDBNull?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码比我自动将 IsDBNull 设置为或只是滚动



这是我的代码:

  Dim conn As New SqlConnection 
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()
结束如果
下一个
$ / pre>

在这种情况下,我只想键入 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 the If statement, and if column IsDBNull then output an empty string.

How can I do this?

Many thanks in advance!

解决方案

You could simply use String.Join and pass 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.

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).

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆