【问题标题】:'An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll' error message?'Microsoft.VisualBasic.dll 中出现'System.InvalidCastException' 类型的未处理异常' 错误消息?
【发布时间】:2015-12-14 20:51:26
【问题描述】:

我做了一个测验,应该将学生的姓名和分数保存到 access 数据库中的正确表中。有3张桌子。我尝试连接数据库并对数据库进行编码,但不断收到以下错误消息:

Microsoft.VisualBasic.dll 中出现“System.InvalidCastException”类型的未处理异常 附加信息:从字符串“INSERT INTO Class1([StudentScor”)转换为“Double”类型无效。

我试图找到解决此错误的方法,但是我并不完全理解。这是我保存按钮的代码,它应该将名称和分数保存到正确的表中:

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    StudentClass = cbSelectClass.SelectedItem
    Provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
    DataFile = IO.Path.Combine(Application.StartupPath, "StudentScores.accdb")
    ConnString = Provider & DataFile
    If StudentClass = "Class1" Then
        Strng = "INSERT INTO Class1 ([StudentScores], [Score]) Values('" + CType(txtName.Text, String) + CType(Score, Integer)
    ElseIf StudentClass = "Class2" Then
        Strng = "INSERT INTO Class2 ([Student Scores], [Score]) Values('" + CType(txtName.Text, String) + CType(Score, Integer)
    ElseIf StudentClass = "Class3" Then
        Strng = "INSERT INTO Class3([StudentScores], [Score]) Values('" + CType(txtName.Text, String) + CType(Score, Integer)
    End If
    MyConnection.ConnectionString = ConnString
    Dim cmd As OleDbCommand = New OleDbCommand(Strng, MyConnection)
    MyConnection.Open()
    Try
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        MyConnection.Close()
        txtName.Clear()
    Catch ex As Exception
        MsgBox(ex.Message)
        cmd.Dispose()
        MyConnection.Close()
    End Try
    Me.Close()
End Sub

【问题讨论】:

    标签: vb.net ms-access


    【解决方案1】:

    你有很多不必要的 CType 强制转换。 Textbox.Text 已经是字符串类型。您不想将 Score 转换为 Integer,因为您要连接到字符串。此外,请习惯 String.Format 函数,因为它有助于代码的可读性,并帮助您找到错误命名的 DB 列名称。不要害怕留白以提高可读性:

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    
        Const SQL_INSERT As String = "INSERT INTO {0} (StudentScores, Scores) VALUES ('{1}', {2})"
    
        Provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
        DataFile = IO.Path.Combine(Application.StartupPath, "StudentScores.accdb")
        ConnString = Provider & DataFile
    
        Dim sql As String = String.Format(SQL_INSERT, cbSelectClass.SelectedItem, txtName.Text.Trim, Score.ToString)
    
        MyConnection.ConnectionString = ConnString
        Dim cmd As OleDbCommand = New OleDbCommand(sql, MyConnection)
        MyConnection.Open()
        Try
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            MyConnection.Close()
            txtName.Clear()
        Catch ex As Exception
            MsgBox(ex.Message)
            cmd.Dispose()
            MyConnection.Close()
        End Try
        Me.Close()
    End Sub
    

    现在,您必须研究 SQL 注入来纠正这个问题,并且您应该使用 Using 语句来初始化 Connection 和 Command 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 2022-07-19
      • 2012-10-29
      • 1970-01-01
      • 2017-06-30
      相关资源
      最近更新 更多