0

我在尝试运行我的代码时遇到问题。我是stackoverflow的新手,但会尝试解释我的问题。

好吧,我得到一个带有解释的“InvalidCastExeption”

“从字符串“文件名”到类型“整数”的转换无效。

令人困惑的部分是 FileName 是我的 SQL 数据库中的 nvarchar(30) ,而 sName 是一个字符串。

看看代码,它在暗淡的 sName = ... Ps 处失败 - 我试图转换.toint / Cint,但它没有帮助。而且我想要它作为一个字符串,而不是一个整数!

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class SelectrecipieDatabaseManager
    Private Const CONNECTION_STRING As String = "Data Source=RYOW701;Initial Catalog=RyReci;Integrated Security=True"
    Private connection As SqlConnection = Nothing
    Private command As SqlCommand = Nothing

    Public Sub RecipieHandler(ByVal Choice As String)

        Dim reader As SqlDataReader
        connection = New SqlConnection(CONNECTION_STRING)

        Try
            connection.Open()


                Dim Query As String
                Query = "select *  from TbNew"
                command = New SqlCommand(Query, connection)
                reader = command.ExecuteReader

                While reader.Read

                    Dim sName = reader.GetString("FileName")

                    RecipieForm.ComboBoxRec.Items.Add(sName)
                End While



        Catch ex As SqlException
            MessageBox.Show(ex.Message)

        Finally
            connection.Close()
            connection.Dispose()
        End Try

    End Sub

End Class
4

1 回答 1

0

getString方法需要一个序数作为它的参数,而不是一个字符串。改为这样做:

reader = command.ExecuteReader
Dim columnID As Integer = reader.GetOrdinal("FileName")

While reader.Read
    Dim sName = reader.GetString(columnID)
    RecipieForm.ComboBoxRec.Items.Add(sName)
End While
于 2014-03-23T18:49:37.857 回答