【发布时间】: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
【问题讨论】: