从"DBNull"类型到"String"类型的转换

从

问题描述:

大家好..

我的代码有问题.我有一个错误
从类型"DBNull"到类型"String"的转换无效."
我只是想不出问题出在哪里.
请帮帮我...

继承人代码:

hi guys ..

I have a problem with this code.I Have an error
"Conversion from type ''DBNull'' to type ''String'' is not valid."
I just cant figure it out wheres the problem.
Please help me...

heres the code:

Private Sub BackupMySql()

        Dim localDir As String = "C:/"
        Dim strDate As String = Date.Now.ToShortDateString   'Prepend file with date for dated backups
        'Dim fileName As String = strDate.Replace("/", "-") & "_" & ftphost & ".sql"
        Dim fileName As String = strDate.Replace("/", "-") & "_" & "test" & ".sql"
        Dim saveFile As String = localDir & fileName
        Dim DBServer As String = "localhost"
        Dim DBServerPort As String = "3306"
        Dim Database As String = "dtr"
        Dim DBUser As String = "root"
        Dim DBPass As String = "pinoy"
        Dim MyConString As String = "SERVER=" & DBServer & ";DATABASE=" & Database & ";UID=" & DBUser & ";PASSWORD=" & DBPass & ";PORT=" & DBServerPort & ";charset=utf8"
        Dim connection As New MySqlConnection(MyConString)
        Dim tablesCommand As MySqlCommand = connection.CreateCommand()
        Dim rowsCommand As MySqlCommand = connection.CreateCommand()
        Dim tablesReader As MySqlDataReader
        Dim rowsReader As MySqlDataReader

        tablesCommand.CommandText = "SHOW TABLES FROM " & Database
        connection.Open()
        tablesReader = tablesCommand.ExecuteReader()

        Dim query As String = ""
        Dim tablesArray As New ArrayList()

        While tablesReader.Read()
            tablesArray.Add(tablesReader.GetValue(0).ToString())
        End While

        tablesReader.Close()
        For i = 0 To tablesArray.Count - 1
            query += "DROP TABLE IF EXISTS `" & tablesArray(i) & "`;" & vbCrLf '& Database & "."
            'query += "DROP TABLE IF EXISTS `" & tablesArray(i) & "`;" & vbCrLf '& Database & "."
            query += vbCrLf & "CREATE TABLE `" & tablesArray(i) & "` (" & vbCrLf
            rowsCommand.CommandText = "DESCRIBE " & tablesArray(i)
            rowsReader = rowsCommand.ExecuteReader()

            Dim temp As String = ""
            While rowsReader.Read()
                query += "`" & rowsReader.GetString("Field") & "` " & rowsReader.GetString("Type")
                If Not rowsReader.GetString("Null") = "YES" Then
                    query += " NOT NULL"
                End If
                If IsDBNull(rowsReader.Item("Default")) = False Then
                    query += " DEFAULT '" & rowsReader.GetString("Default") & "'"
                End If
                If Not rowsReader.GetString("Extra") = Nothing Then
                    query += " " & rowsReader.GetString("Extra").ToUpper()
                End If
                If rowsReader.GetString("Key") = "PRI" Then
                    temp = "primary key(" & rowsReader.GetString("Field") & ")"
                End If
                query += "," & vbCrLf

            End While
            query += temp & vbCrLf & ");" & vbCrLf & vbCrLf
            rowsReader.Close()
            rowsCommand.CommandText = "SELECT * FROM " & tablesArray(i)
            rowsReader = rowsCommand.ExecuteReader()

            While rowsReader.Read()
                query += "INSERT INTO `" & tablesArray(i) & "` ("
                Dim count As Integer = rowsReader.FieldCount - 1
                Dim keys(count) As String
                Dim values(count) As String
                For n = 0 To count
                    keys(n) = rowsReader.GetName(n)
                    values(n) = rowsReader.Item(n)
                Next
                query += Join(keys, ", ") & ")" & vbCrLf & "VALUES ('" & Join(values, "', '") & "');" & vbCrLf
            End While
            rowsReader.Close()
            query += vbCrLf & vbCrLf

        Next
        connection.Close()
        connection.Dispose()

        If File.Exists(saveFile) Then
            File.Delete(saveFile)
        End If
        Dim objWriter As New System.IO.StreamWriter(saveFile)

        objWriter.Write(query)
        objWriter.Close()

    End Sub



请帮帮我...
感谢高级.!



Please help me...
Thanks in advanced.!

发生的是数据库中某一行中的一列为空.当单元格为空时,SQL不会发送",而是发送值DBNull.如果表中可以有空单元格,则需要检查每个单元格的DBNull.您可以使用IsDBNull进行检查,如果不是,则可以测试您的值.

您应该查看所使用的每个对象的文档,特别是 ^ ]

它说:在调用此方法之前,请调用IsDBNull以检查空值."

代替:
What''s happening is that a column in one of the rows in your database is empty. When a cell is empty, SQL doesn''t send "", it sends the value DBNull. If your table can have empty cells, then you need to check each cell for DBNull. You can use IsDBNull to check that and if it isn''t, then you can test your values.

You should look at the documentation for each object your using, specifically MySqlDataReader.GetString()[^]

It says, "Call IsDBNull to check for null values before calling this method."

Instead of:
If Not rowsReader.GetString("Null") = "YES" Then
     query += " NOT NULL"
End If


试试:


try:

If Not rowsReader.IsDBNull("Null") Then
    If Not rowsReader.GetString("Null") = "YES" Then
        query += " Not Null"
    End If
End If



查看一些资源,我相信您也可以写



Looking at some resources, I believe you could also write

If (If(Not rowsReader.IsDBNull("Null"), Not rowsReader.GetString("Null") = "YES", False)) Then
  query += " Not Null"
End If


我怀疑您的错误行在这里;

I suspect your error lines here;

jleonorlane写道:
jleonorlane wrote:

values(n)= rowsReader.Item(n)

values(n) = rowsReader.Item(n)



您需要先测试System.DBNull的类型,然后再测试.ToString().

在Google上搜索"DBNull VB.Net",那里有上百万个示例.



You will need to test for type of System.DBNull and then .ToString() it.

Search google for "DBNull VB.Net" there are a million examples there.