2014-05-25 48 views
0

我能夠連接到數據庫,但是當我嘗試插入我得到這個神祕的錯誤:MySQL的插入導致了個MySqlException上.ExecuteNonQuery

已發生錯誤0:命令執行過程中遇到致命錯誤。

我檢查過,我的所有參數都有值,它們與列標題完全匹配,除了自動增量的ID。

我要問哪裏錯了?

Dim iReturn As Boolean 

    Dim conn As New MySqlConnection 
    Dim cmd As New MySqlCommand 

    Dim strConnection = "server=" & txtServer.Text & ";" _ 
       & "user id=" & txtUsername.Text & ";" _ 
       & "password=" & txtPassword.Text & ";" _ 
       & "database=" & txtDatabase.Text 

    conn.ConnectionString = strConnection 

    Try 
     conn.Open() 
     cmd.Connection = conn 

     cmd.CommandText = "INSERT INTO twc_data VALUES(@todaysdate,@fname,@mname,@lname,@address,@city,@state,@zip,@email,@arPhone(0),@arPhone(1),@arPhone(2),@arCategory(0),@arCategory(1),@arJob1(1),@arJob1(2),@arJob1(3),@arJob1(4),@arJob1(5),@arJob2(1),@arJob2(2),@arJob2(3),@arJob2(4),@arJob2(5),@arJob3(1),@arJob3(2),@arJob3(3),@arJob3(4),@arJob3(5),@arCategory(2),@arCategory(3),@arCategory(4),@arCategory(5),@arCategory(6),@arCategory(7),@arCategory(8),@arCategory(9),@arCategory(10),@pdfilename,@strText)" 

     cmd.Prepare() 

     With cmd 
      .Prepare() 
      .Parameters.AddWithValue("@todaysdate", param(0)) 
      .Parameters.AddWithValue("@fname", param(1)) 
      .Parameters.AddWithValue("@mname", param(2)) 
      .Parameters.AddWithValue("@lname", param(3)) 
      .Parameters.AddWithValue("@address", param(4)) 
      .Parameters.AddWithValue("@city", param(5)) 
      .Parameters.AddWithValue("@state", param(6)) 
      .Parameters.AddWithValue("@zip", param(7)) 
      .Parameters.AddWithValue("@email", param(8)) 
      .Parameters.AddWithValue("@arPhone(0)", param(9)) 
      .Parameters.AddWithValue("@arPhone(1)", param(10)) 
      .Parameters.AddWithValue("@arPhone(2)", param(11)) 
      .Parameters.AddWithValue("@arCategory(0)", param(12)) 
      .Parameters.AddWithValue("@arCategory(1)", param(13)) 
      .Parameters.AddWithValue("@arJob1(1)", param(14)) 
      .Parameters.AddWithValue("@arJob1(2)", param(15)) 
      .Parameters.AddWithValue("@arJob1(3)", param(16)) 
      .Parameters.AddWithValue("@arJob1(4)", param(17)) 
      .Parameters.AddWithValue("@arJob1(5)", param(18)) 
      .Parameters.AddWithValue("@arJob2(1)", param(19)) 
      .Parameters.AddWithValue("@arJob2(2)", param(20)) 
      .Parameters.AddWithValue("@arJob2(3)", param(21)) 
      .Parameters.AddWithValue("@arJob2(4)", param(22)) 
      .Parameters.AddWithValue("@arJob2(5)", param(23)) 
      .Parameters.AddWithValue("@arJob3(1)", param(24)) 
      .Parameters.AddWithValue("@arJob3(2)", param(25)) 
      .Parameters.AddWithValue("@arJob3(3)", param(26)) 
      .Parameters.AddWithValue("@arJob3(4)", param(27)) 
      .Parameters.AddWithValue("@arJob3(5)", param(28)) 
      .Parameters.AddWithValue("@arCategory(2)", param(29)) 
      .Parameters.AddWithValue("@arCategory(3)", param(30)) 
      .Parameters.AddWithValue("@arCategory(4)", param(31)) 
      .Parameters.AddWithValue("@arCategory(5)", param(32)) 
      .Parameters.AddWithValue("@arCategory(6)", param(33)) 
      .Parameters.AddWithValue("@arCategory(7)", param(34)) 
      .Parameters.AddWithValue("@arCategory(8)", param(35)) 
      .Parameters.AddWithValue("@arCategory(9)", param(36)) 
      .Parameters.AddWithValue("@arCategory(10)", param(37)) 
      .Parameters.AddWithValue("@pdfilename", param(38)) 
      .Parameters.AddWithValue("@strText)", param(39)) 
     End With 

     cmd.ExecuteNonQuery() 
     iReturn = True 
    Catch ex As MySqlException 
     param(40) = "Error " & ex.Number & " has occurred: " & ex.Message 
     logError() 
     iReturn = False 
    Finally 
     conn.Close() 
    End Try 

    Return iReturn 
+0

副本的SQL代碼爲字符串,並嘗試直接在我的SQL Server – Jackson

+0

上執行它是否真的有*有關異常沒有*其他信息?您似乎在扔掉潛在的有用信息,例如堆棧跟蹤或任何潛在的內部異常或「MySqlException」中的任何其他屬性。不要扔掉有關錯誤的信息。 – David

+1

參數名稱中是否允許使用圓括號?即使它們是*無與倫比的*括號允許? (請參閱參數39)嘗試不使用所有這些括號,也許這些是數據庫的語法錯誤。 – David

回答

0

您應該指定帶有ID的表的列名。

INSERT INTO twc_data([column names of the table]) VALUES(@todaysdate,.....