如何将dbnull类型转换为字符串

时间:2015-03-17 15:54:44

标签: vb.net-2010

If conn.State = ConnectionState.Open Then
    conn.Close()
End If
conn.Open()
sql = "SELECT *FROM tbL_books_transactions WHERE transactionID LIKE '%" & TextBox8.Text & "%'"
cmd = New MySqlCommand(sql, conn)

Dim dr As MySqlDataReader

dr = cmd.ExecuteReader
If dr.Read = True Then
    TextBox4.Text = CType(dr("AccessionNo"), String)
    TextBox7.Text = CType(dr("BookTitle"), String)
    TextBox11.Text = CType(dr("DateBorrowed"), String).ToString()
    TextBox3.Text = CType(dr("userID"), String)
    TextBox10.Text = CType(dr("userName"), String)

Else
    MsgBox("No Student Record Exist")

End If
conn.Close()

1 个答案:

答案 0 :(得分:0)

这是一种将包含空值的列转换为可以使用文本框的简单方法

Dim pos = dr.GetOrdinal("AccessioNo")
TextBox4.Text = if(dr.IsDbNull(pos), string.Empty, dr.GetString(pos))

在此示例中,首先检索您有兴趣阅读的列的序号位置。然后使用该值调用IsDbNull并将所有内容放在ternary operator内如果valus为null,则返回一个空字符串,否则返回带有GetString的字段的内容。