【问题标题】:'Value of type string cannot be converted to system.data.datatable' in vb.netvb.net 中的“字符串类型的值不能转换为 system.data.datatable”
【发布时间】:2012-08-16 11:47:31
【问题描述】:

这是我的代码。我一直有一个错误“字符串类型的值无法转换为 system.data.datatable”

Function GetTable() As DataTable
        Dim SQLConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("Zeinchatconnection").ToString())
        Dim CommSQL As New SqlClient.SqlCommand
        Dim ChatDataAdapter As SqlDataAdapter
        Dim paramSQL As SqlClient.SqlParameter
        Dim DStable As DataSet
        Dim table As New DataTable
        Dim szName As String = ""
        Dim szNumber As String = ""
        Try
            If SQLConnection.State = ConnectionState.Closed Then
                SQLConnection.Open()
            End If
            CommSQL.Connection = SQLConnection
            CommSQL.CommandType = CommandType.StoredProcedure
            CommSQL.CommandText = "spc_newselect"



        CommSQL.ExecuteNonQuery()

        ChatDataAdapter = New SqlDataAdapter(CommSQL)
        ChatDataAdapter.Fill(DSTable)

        table.Rows.Clear()
        table.Clear()
        table = DStable.Tables(0)

        Dim i As Integer = 0

        For i = 0 To table.Rows.Count - 1
            szName = szName & "  " & table.Rows(i).Item(0) & table.Rows(i).Item(1)
            szNumber = szNumber & "  " & table.Rows(i).Item(0) & table.Rows(i).Item(1)
        Next

        GetTable = "1"
    Catch ex As System.Data.SqlClient.SqlException
        GetTable = "0"
    Catch ex As Exception
        GetTable = "0"

        If (IsNothing(ChatDataAdapter) = False) Then ChatDataAdapter.Dispose()
        If (IsNothing(CommSQL) = False) Then CommSQL.Dispose()
        SQLConnection.Close()
    End Try
    Return table


End Function

出错的部分是gettable="1" 及以下。

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    GetTable = "1" 表示要设置函数的returnValue。由于您的函数定义为Function GetTable() As DataTable,您的编译器会显示错误!

    下面几行有一个正确的 return stmt (Return table) 所以我不太确定你对GetTable = "1" 的目标是什么?

    我假设您希望有一个额外的 returnValue 来指示您的函数调用是否成功。但事实上函数可能只有一个 returnValue。

    您可以选择将表变量设置为空,或使用 ref 参数...

    ' possible solution 1 - using nothing value
    Function GetTable() As DataTable
        Try
            ' your code goes here
            ' remove GetTable = "1" 
        Catch ex as Exception
            ' change GetTable = "0" to 
            table = nothing
        End Try
        ' other code ...
    End Function
    
    ' possible solution 2 - ref param
    Function GetTable(ByRef status as integer) as DataTable
       Try
            ' your code goes here
            ' remove GetTable = "1" 
            status = 1
        Catch ex as Exception
            ' change GetTable = "0" to 
            status = 0
        End Try
        ' other code ...
    End Function
    

    在解决方案 2 中,您还可以选择一个 布尔参数,指示您的调用是否成功。

    【讨论】:

    • 如果您发现我的答案有用,您可以考虑将其标记为“最佳/有用的答案”,或者投票赞成任何对您有帮助的答案 - 提前谢谢
    • “不返回任何内容”很有用,应该避免使用“byref”方法。赞成阻止社区用户提出这个古老的、相对无用的问题
    【解决方案2】:

    您的函数GetTable 返回一个DataTable

    您无法将"1" 转换为DataTable,因为消息表明GetTable = "1" 线上正在发生的事情

    如果你想返回一个DataTable 并设置一个标志来查看状态;改变你的函数定义:

    Function GetTable(byref result as Integer) As DataTable
    

    然后将 GetTable = "1" 改为 result = 1,而不是 result = 1。这样您就可以检查结果值并返回一个 DataTable:

    Dim res as Integer
    Dim dt as DataTable = GetTable(res)
    If res = 1 then
        'It worked!
    End If
    

    旁注:Turn Option Strict On

    【讨论】:

    • 实际上我删除了 get table 所以它工作了谢谢你的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 2021-06-19
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多