vb.net读取mysql表列无论mysql中的哪个字段类型

时间:2016-02-05 21:51:05

标签: mysql vb.net

我在阅读器循环中运行此代码:

While reader.read
For x = 0 To reader.FieldCount - 1
    MsgBox(reader.GetValue(x))
    Dim find_text As String = "<<" & reader.GetName(x) & ">>"
    Dim replacet_text As String = reader.GetString(x)

    oDoc.Content.Find.Execute(FindText:=find_text, ReplaceWith:=replacet_text, Replace:=word.WdReplace.wdReplaceAll)
Next
End While

所以它显示每列作为我的SQL查询说SELECT * from table1

但问题是,根据数据库中列的类型显示错误,如cannot be converted to type 'String'.

无论什么类型,我怎么能做所有列?我只想把所有内容都读成字符串

1 个答案:

答案 0 :(得分:1)

尝试Dim replacet_text As String = reader.GetValue(x).ToString如果失败则意味着该类型不能转换为字符串,因此您可以将此行代码放在try catch块中,并在数据无法转换为字符串时发出错误消息:

While reader.read
For x = 0 To reader.FieldCount - 1
    MsgBox(reader.GetValue(x))
    Dim find_text As String = "<<" & reader.GetName(x) & ">>"
    Dim replacet_text As String
    Try
        replacet_text = reader.GetValue(x).ToString()
    Catch ex As Exception
        replacet_text = "-- Not Available! --"
    End Try
    oDoc.Content.Find.Execute(FindText:=find_text, ReplaceWith:=replacet_text, Replace:=word.WdReplace.wdReplaceAll)
Next
End While