所以我得到了更新语句,并且正在更新数据库中的记录。但是,该程序在更新记录后会抛出错误。
错误
未处理的类型' System.Data.OleDb.OleDbException'发生在System.Data.dll
中其他信息:您向表请求的更改未成功,因为它们会在索引,主键或关系中创建重复值。更改包含重复数据的字段或字段中的数据,删除索引或重新定义索引以允许重复条目,然后重试。
代码
Dim variable As String
dsConnectionM.Open()
variable = "UPDATE Member SET MemberID = '" & Form3.tbid.Text & "', Forename = '" & Form3.tbfn.Text & "' "
Dim cmd As OleDbCommand = New OleDbCommand(variable, dsConnectionM)
cmd.ExecuteNonQuery()
dsConnectionM.Close()
I don't know if this is normal but when ran a second Access file for the database is created
This shows the contents of "Member" in the databse
EDITED
我开始使用参数,因为我被告知现在我得到了另一个错误。
错误
未处理的类型' System.Data.OleDb.OleDbException'发生在System.Data.dll
中其他信息:UPDATE语句中的语法错误。
CODE
Dim variable As String
dsConnectionM.Open()
variable = "UPDATE Member
SET Forename = @Forename,
Surname = @Surname,
DOB = @DOB,
Section = @Section,
Postcode = @Postcode,
HomeTel = @HomeTel,
MobileTel = @MobileTel,
AddressLine1 = @AddressLine1,
AddressLine2 = @AddressLine2,
City = @City,
EmailAddress = @EmailAddress,
P/GForename = @P/GForename,
P/GSurname = @P/GSurname
WHERE MemberID = @MemberID"
Dim cmd As OleDbCommand = New OleDbCommand(variable, dsConnectionM)
cmd.Parameters.AddWithValue("@MemberID", Form3.tbid.Text)
cmd.Parameters.AddWithValue("@Forename", Form3.tbfn.Text)
cmd.Parameters.AddWithValue("@Surname", Form3.tbsn.Text)
cmd.Parameters.AddWithValue("@DOB", Form3.dtpdob.Value)
cmd.Parameters.AddWithValue("@Section", Form3.tbsr.Text)
cmd.Parameters.AddWithValue("@Postcode", Form3.tbpc.Text)
cmd.Parameters.AddWithValue("@HomeTel", Form3.tbht.Text)
cmd.Parameters.AddWithValue("@MobileTel", Form3.tbmt.Text)
cmd.Parameters.AddWithValue("@AddressLine1", Form3.tbal1.Text)
cmd.Parameters.AddWithValue("@AddressLine2", Form3.tbal2.Text)
cmd.Parameters.AddWithValue("@City", Form3.tbc.Text)
cmd.Parameters.AddWithValue("@EmailAddress", Form3.tbea.Text)
cmd.Parameters.AddWithValue("@P/GForename", Form3.tbpgfn.Text)
cmd.Parameters.AddWithValue("@P/GSurname", Form3.tbpgsn.Text)
cmd.ExecuteNonQuery()
dsConnectionM.Close()
答案 0 :(得分:1)
通常您不会UPDATE
表格的Pk
。在您的表单中,ID
应该是只读的。如果有人更改了其他值,您可以使用ID
查找到UPDATE
variable = "UPDATE Member " &
"SET Forename = '" & Form3.tbfn.Text & "' "
"WHERE MemberID = '" & Form3.tbid.Text & "'
你需要使用参数,否则你很容易受到SQL注入攻击
答案 1 :(得分:0)
我假设您的查询是为了更改具有特定ID的成员的Forename字段。
在这种情况下,您的查询应该如下所示
UPDATE Member SET Forename = '" & Form3.tbfn.Text & "' where MemberID = '" & Form3.tbid.Text & "'