0
我已經做了一個測驗,應該將學生姓名和分數保存到訪問數據庫中的正確表格中。有3張桌子。我試圖連接和代碼數據庫,但我不斷收到以下錯誤信息:'在Microsoft.VisualBasic.dll'錯誤消息中發生未處理的類型'System.InvalidCastException'異常?
「」類型的未處理的異常出現在Microsoft.VisualBasic.dll中 其他信息:從字符串轉換「INSERT到Class([StudentScor」鍵入‘雙師型’是無效的。
我試圖找到這個錯誤的解決方案不過,我不完全理解。這裏是我的保存按鈕,應該保存代碼名稱和分數到正確的表格:
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