0

これは私のコードです。「string 型の値を 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" 以下です。

4

2 に答える 2

1

GetTable = "1"関数の returnValue を設定することを示します。関数がFunction GetTable() As DataTableコンパイラとして定義されているため、エラーが表示されます。

その下の数行には正しい return stmt( Return table) があるので、あなたの目標が何なのかよくわかりませんGetTable = "1"

関数呼び出しが成功したかどうかを示す追加の returnValue が必要だと思います。しかし実際のところ、関数は returnValue を 1 つしか持てません。

テーブル var を何も設定しないか、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 では、呼び出しが成功したかどうかを示すブール値パラメーターを選択することもできます。

于 2012-08-16T11:56:15.727 に答える
0

あなたの関数GetTableDataTable

メッセージが示すように、ラインで何が起こっているか"1"をaに変換することはできませんDataTableGetTable = "1"

戻りたい場合DataTableはフラグを設定してステータスを確認します。関数定義を次のように変更します。

Function GetTable(byref result as Integer) As DataTable

GetTable = "1"次に、これをに変更する代わりにresult = 1。このようにして、結果の値を検査し、DataTable を返すことができます。

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

補足: Option Strict をオンにする

于 2012-08-16T11:55:43.843 に答える