我怎样摆脱:
System.InvalidCastException:'从字符串“Type”转换为type '整数'无效。'“
在线:
Dim usertype = Reader.GetString("Type")
这是我的完整代码:
Private Sub OKbt1_Click(sender As Object, e As EventArgs) Handles OKbt1.Click
Call Connect() ' [ connection to module ]'
Dim Reader As SqlDataReader
Try
Dim command As New SqlCommand("select * from uinfo where password = '" & PASStb2.Text & "'", sqlConn)
Reader = command.ExecuteReader
Reader.Read()
Dim count As Integer = 0
While Reader.Read
count = count + 1
End While
If count = 1 Then
' ** MY ERROR **
Dim usertype = Reader.GetString("Type")
If usertype = "admin" Then
'MsgBox("username and password are correct")
MAIN_MENU.Show()
For a = 0 To 500
Next
Me.Hide()
sqlConn.Close()
sqlConn.Dispose()
ElseIf usertype = "user" Then
For a = 0 To 500
Next
Me.Hide()
'MsgBox("username and password are correct")
USERMENU.Show()
End If
ElseIf count > 1 Then
MsgBox("username and password are duplicate")
Else
MsgBox("username and password are not correct")
End If
sqlConn.Close()
Catch ex As SqlException
MsgBox(ex.Message)
Finally
sqlConn.Dispose()
End Try
End Sub
答案 0 :(得分:0)
'Reader.GetString'接受整数参数,而不是字符串。或者,做
Reader.Item("Type").ToString()
答案 1 :(得分:0)
将usertype声明为Dim usertype作为String,然后为其赋值,如usertype = Reader.Item(“Type”)。ToString()。 你需要通过Reader.GetString(“Type”)检查值返回它可能为null。启用“ Option Strict”开启从长远来看,这将有所帮助。
答案 2 :(得分:0)
**Dim usertype = Reader.GetString("Type") // MY ERROR**
您传递的字符串值显然是错误的
GetString函数接受整数。 您可能希望实例化一个整数数据并通过getstring传递它。
Dim usertype = Reader.GetString(data)
答案 3 :(得分:0)
这是因为您可能无法正确地从“Reader”对象获取数据
请改为尝试:
Dim command As SqlCommand = New SqlCommand("SELECT * FROM uinfo WHERE password = '" & PASStb2.Text & "'", connection)
connection.Open()
Dim READER As SqlDataReader = command.ExecuteReader()
If READER.HasRows Then
While READER.Read()
Console.WriteLine("{0}" & vbTab & "{1}", READER.GetInt32(0), READER.GetString(1))
End While
Else
Console.WriteLine("No rows found.")
End If
READER.Close()
答案 4 :(得分:0)
使用参数。我让服务器进行计数。为您节省一些代码。
Private Sub OKbt1_Click(sender As Object, e As EventArgs) Handles OKbt1.Click
Call Connect() ' [ connection to module ]'
Dim Reader As SqlDataReader
Try
Using cmd As New SqlCommand("select usertype, Count(*) from uinfo Group By usertype where [password] = @password;", sqlConn)
cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = PASStb2.Text
Reader = cmd.ExecuteReader()
If Reader.HasRows Then
Reader.Read()
Dim usertype As String = Reader.GetString(0)
Dim count As Integer = Reader.GetInt32(1)
If count = 1 Then
If usertype = "admin" Then
MAIN_MENU.Show()
Hide()
ElseIf usertype = "user" Then
USERMENU.Show()
Hide()
End If
ElseIf count > 1 Then
MsgBox("username and password are duplicate")
End If
Else
MsgBox("username and password are not correct")
End If
End Using
Catch ex As Exception
MsgBox(ex.Message)
Finally
sqlConn.Close()
sqlConn.Dispose()
End Try
End Sub
答案 5 :(得分:0)
SqlDataReader.GetString Method (Int32)需要一个整数(列索引)作为参数。所以你需要
Dim usertype as String = Cstr(Reader("Type"))
或
Dim usertype = Reader.GetString(Reader.GetOrdinal("Type"))
或
Dim usertype = Reader.GetFieldValue(Of String)("Type")
请注意,没有那些posibilites可以处理DBnull。