2015-04-22 86 views
1

在此代碼中出現錯誤,該代碼旨在創建Access數據庫中的記錄,但似乎無法解決爲什麼。在System.Data.dll中發生類型爲「System.Data.OleDb.OleDbException」的未處理異常

Option Explicit On 
Option Strict On 

Imports System.Data.OleDb 

'Name:   CustomerController.vb 
'Description: Class acting as intermediary between the Customer Form and Customer table 
'    Contains Most of the CRUD business Logic 
'Author:  Alastair McIntyre 
'Date:   12/04/2015 

Public Class CustomerController 
    Public Const CONNECTION_STRING As String = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=assignment 1.accdb" 

    Public Function insertCustomer(ByVal htCustomer As Hashtable) As Integer 

     Dim oConnection As OleDbConnection = New OleDbConnection(CONNECTION_STRING) 
     Dim iNumRows As Integer 
     Try 
      Debug.Print("Connection string: " & oConnection.ConnectionString) 

      oConnection.Open() 
      Dim oCommand As OleDbCommand = New OleDbCommand 
      oCommand.Connection = oConnection 

      oCommand.CommandText = _ 
       "INSERT INTO customer (title, gender, firstname, lastname, phone, address, email, dob) VALUES (?, ?, ?, ?, ?, ?, ?, ?);" 

      oCommand.Parameters.Add("title", OleDbType.VarChar, 255) 
      oCommand.Parameters.Add("gender", OleDbType.VarChar, 255) 
      oCommand.Parameters.Add("firstname", OleDbType.VarChar, 255) 
      oCommand.Parameters.Add("lastname", OleDbType.VarChar, 255) 
      oCommand.Parameters.Add("phone", OleDbType.Integer, 11) 
      oCommand.Parameters.Add("address", OleDbType.VarChar, 255) 
      oCommand.Parameters.Add("email", OleDbType.VarChar, 255) 
      oCommand.Parameters.Add("dob", OleDbType.Integer, 8) 

      oCommand.Parameters("title").Value = CStr(htCustomer("title")) 
      oCommand.Parameters("gender").Value = CStr(htCustomer("gender")) 
      oCommand.Parameters("firstname").Value = CStr(htCustomer("firstname")) 
      oCommand.Parameters("lastname").Value = CStr(htCustomer("lastname")) 
      oCommand.Parameters("phone").Value = CInt(htCustomer("phone")) 
      oCommand.Parameters("address").Value = CStr(htCustomer("address")) 
      oCommand.Parameters("email").Value = CStr(htCustomer("email")) 

      oCommand.Prepare() 

      iNumRows = oCommand.ExecuteNonQuery() 
      Debug.Print(CStr(iNumRows)) 

      Debug.Print("The record was insterted") 
      'Catch ex As Exception 
      'Debug.Print("Error: " & ex.Message) 
      'MsgBox("An error occured. The record wasn't inserted") 
     Finally 
      oConnection.Close() 
     End Try 

     Return iNumRows 
    End Function 
End Class 

註釋掉錯誤消息,試圖調試錯誤後,我發現這裏「http://i.stack.imgur.com/fQgBJ.png」發生錯誤如果發生這種情況在德巴模式下,應用程序崩潰,並且不產生在一個記錄鏈接數據庫

+0

今後請包括錯誤消息和它在你的文章中發生的位置,而不是錯誤圖片的鏈接 – Plutonix

回答

1

讀取錯誤消息:

參數_8沒有默認值。

您所查詢的有8個參數佔位符:

oCommand.CommandText = _ 
      "INSERT INTO customer (title, gender, firstname, lastname, phone, address, email, dob) VALUES (?, ?, ?, ?, ?, ?, ?, ?);" 

然後添加8個參數:

oCommand.Parameters.Add("title", OleDbType.VarChar, 255) 
oCommand.Parameters.Add("gender", OleDbType.VarChar, 255) 
oCommand.Parameters.Add("firstname", OleDbType.VarChar, 255) 
oCommand.Parameters.Add("lastname", OleDbType.VarChar, 255) 
oCommand.Parameters.Add("phone", OleDbType.Integer, 11) 
oCommand.Parameters.Add("address", OleDbType.VarChar, 255) 
oCommand.Parameters.Add("email", OleDbType.VarChar, 255) 
oCommand.Parameters.Add("dob", OleDbType.Integer, 8) 

然後......你設置7個值:

oCommand.Parameters("title").Value = CStr(htCustomer("title")) 
oCommand.Parameters("gender").Value = CStr(htCustomer("gender")) 
oCommand.Parameters("firstname").Value = CStr(htCustomer("firstname")) 
oCommand.Parameters("lastname").Value = CStr(htCustomer("lastname")) 
oCommand.Parameters("phone").Value = CInt(htCustomer("phone")) 
oCommand.Parameters("address").Value = CStr(htCustomer("address")) 
oCommand.Parameters("email").Value = CStr(htCustomer("email")) 

你需要爲第8個參數(「dob」)設置一個值。

(附註:手機和DOB可能不應該整數因爲他們不是整數電話是文本值,DOB是一個日期值)。

+0

感謝您的新鮮眼睛,它修復了它。這樣一個簡單的錯誤,但經過這麼長時間的工作,我只是看不到它。 –

相關問題