每当我运行代码并尝试运行 SQL 查询时,它都会显示数据类型不匹配代码如下:
Public Class Form7
Public insert As New DBController
Public username, password, ID As String
Public chars(20) As Char
Public cc As Integer
Private Sub BTNSaveRecord_Click(sender As Object, e As EventArgs) Handles BTNSaveRecord.Click
chars = TXTLastName.Text.ToCharArray
ID = chars(0) & chars(1) & chars(2)
insert.AddParameters("@ID", ID)
insert.MakeQuery("SELECT Driver_ID FROM Driver WHERE Driver_ID LIKE '%@ID%'")
ID = ID & (insert.recordcount + 120).ToString
insert.clearDT()
cc = Conversion.Val(TXTCarCapacity.Text)
MsgBox(cc)
insert.AddParameters("@ID", ID)
insert.AddParameters("@Fname", TXTFirstName.Text)
insert.AddParameters("@Lname", TXTLastName.Text)
insert.AddParameters("@CarCap", cc)
insert.AddParameters("@pass", "Password1")
username = TXTFirstName.Text & TXTLastName.Text.First
MsgBox(username)
MsgBox(ID)
insert.AddParameters("@user", username)
insert.MakeQuery("INSERT INTO Driver (Driver_ID,Maximum_Case_Capacity,Driver_Username,Driver_Password,Driver_FirstName,Driver_LastName) " &
"VALUES (@ID,@CarCap,@user,@pass,@Fname,@Lname);")
End Sub
如果你想知道数据库控制器的代码,我有代码。那是一个自定义的类,使 SQL 内容不那么重复。就像数据适配器命令构建器和数据表一样。
这是我正在插入新记录的数据库中的表。这也没有关系,它会阻止我输入新记录。该图像包含每个字段的数据类型类型Maximum_case_capacity
Access does not care what you call your parameters; the names are ignored. It is the order the parameters are added that matches them to the database. Since the order of the fields do not match the order the parameters are added you are getting a type mismatch when Access tries to add the string FName to the numeric field Maximum_Case_Capacity.
insert.AddParameters("@ID", ID)
insert.AddParameters("@CarCap", cc)
username = TXTFirstName.Text & TXTLastName.Text.First
insert.AddParameters("@user", username)
insert.AddParameters("@pass", "Password1")
insert.AddParameters("@Fname", TXTFirstName.Text)
insert.AddParameters("@Lname", TXTLastName.Text)
In a real application, you would never add passwords as plain text. Also, the interesting dance you do to get a unique primary key (maybe) could be avoided if you used a numeric autoincrement field in Access. I am afraid you will run out of space in that field rather quickly as it is only 6 characters wide. Hope you don't get too many people named Smith. ;-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句