我可以很好地连接到数据库,但是当我尝试INSERT时,我得到了这个神秘的错误:
发生错误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 :(得分:0)
您应该使用out ID指定表的列名。
INSERT INTO twc_data([column names of the table]) VALUES(@todaysdate,.....