我的代碼是:
Dim cmdUpdate As New OleDbCommand("Update Account Name'" & txtAccountName.Text & "', Age='" & txtAccountAge.Text & "', Degress='" & cboAccountDegress.Text & "', Birthday='" & txtAccountBirthday.Text & "', Address='" & txtAccountAddress.Text & "', Number='" & txtAccountNumber.Text & "', Gender='" & txtAccountNumber.Text & "' where ID=" & txtAccountID.Text & "", conn)
cmdUpdate.Parameters.AddWithValue("@ID", txtAccountID.Text)
cmdUpdate.Parameters.AddWithValue("@Name", txtAccountName.Text)
cmdUpdate.Parameters.AddWithValue("@Age", txtAccountAge.Text)
cmdUpdate.Parameters.AddWithValue("@Degress", cboAccountDegress.Text)
cmdUpdate.Parameters.AddWithValue("@Birthday", txtAccountBirthday.Text)
cmdUpdate.Parameters.AddWithValue("@Address", txtAccountAddress.Text)
cmdUpdate.Parameters.AddWithValue("@Number", txtAccountNumber.Text)
cmdUpdate.Parameters.AddWithValue("@Gender", cboAccountGender.Text)
conn.Open()
cmdUpdate.ExecuteNonQuery()
conn.Close()
bind_data()
uj5u.com熱心網友回復:
您的 UPDATE 應該使用 Parameter 占位符,例如:
Dim cmdUpdate As New OleDbCommand("Update Account SET [Name]=@Name, Age=@Age, Degress=@Degress, Birthday=@Birthday, Address=@Address, [Number]=@Number, Gender=@Gender where ID=@ID", conn)
此外,您的UPDATE陳述句還缺少SET關鍵字,您還必須注意使用保留字作為列名并用方括號括起任何沖突的列,請參閱我如何更新 UPDATE 陳述句中的Name和Number列
然后更改添加引數的順序,將 Id 引數放在最后:
cmdUpdate.Parameters.AddWithValue("@Name", txtAccountName.Text)
cmdUpdate.Parameters.AddWithValue("@Age", txtAccountAge.Text)
cmdUpdate.Parameters.AddWithValue("@Degress", cboAccountDegress.Text)
cmdUpdate.Parameters.AddWithValue("@Birthday", txtAccountBirthday.Text)
cmdUpdate.Parameters.AddWithValue("@Address", txtAccountAddress.Text)
cmdUpdate.Parameters.AddWithValue("@Number", txtAccountNumber.Text)
cmdUpdate.Parameters.AddWithValue("@Gender", cboAccountGender.Text)
cmdUpdate.Parameters.AddWithValue("@ID", txtAccountID.Text)
對于基于 OleDb 的連接,SQL 陳述句中的引數順序應與引數添加到Parameters集合中的順序一致
uj5u.com熱心網友回復:
您應該使用 SQL Server 更新功能
UPDATE TableName
設定要更新的列名
SET Column1 = @Column1 ,Column2 = @Column2
并使用 where 定義條件
Where Column3 = ‘Condition’
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473618.html
上一篇:VB.Net公共屬性回傳空值
