當我運行我的項目時,它不斷給我提供錯誤。第一個例外是da.Fill(dt)。它說IErrorInfo.GetDescription使用E_FAIL(0x80004005)失敗。 第二個異常是在cmd.ExecuteNonQuery()並且在INSERT INTO語句中表示語法錯誤。 請幫幫我。我對VB非常陌生,所以即使我整天看着它,也不能真正看到問題。當我運行我的項目時,我總是收到錯誤
公共類Telephone_Bill
Dim cnn As New OleDb.OleDbConnection
Private Sub RefreshData()
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT Month as [Month], " & _
"Day as [Day], Year, Amount Paid, Amount Due, Mode of Payment, Company Name " & _
" FROM Transactions ORDER BY Month", cnn)
Dim dt As New DataTable
Transaction_Log.dgvTransaction.DataSource = dt
da.Fill(dt)
cnn.Close()
End Sub
Private Sub btnProceed_Click(sender As System.Object, e As System.EventArgs) Handles btnProceed.Click
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
cmd.CommandText = "INSERT INTO Transactions([Month], [Day], [Year], AmountPaid, AmountDue, ModeofPayment, CompanyName) " & _
" VALUES(" & cboMonth1.SelectedItem & ",'" & cboDay1.SelectedItem & "','" & _
cboYear1.SelectedItem & "','" & txtAmount.Text & "','" & _
txtTotalCharges.Text & "','" & cboMonetary.SelectedItem & "','" & _
txtCompName.Text & "')"
cmd.ExecuteNonQuery()
RefreshData()
cnn.Close()
End Sub
Private Sub Telephone_Bill_Load(sender As Object, e As System.EventArgs) Handles Me.Load
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accounts for Bill Payment Center.accdb"
Me.RefreshData()
End Sub
末級
您在SELECt和INSERT查詢之間的字段名稱有所不同。你有'金額付款'或者你的字段被命名爲'AmountPaid'(對SELECT中的其他字段名稱也需要相同的說明) – Steve
檢查你的查詢。在你指定的列名中使用了refreshdata()並且在commandtext中你沒有空格(例如:支付方式)。實際上什麼是正確的? –
您調用SQL的整個方法都是錯誤的;您需要使用[參數](http://stackoverflow.com/q/11139791/22437)。 –