[英]how to update from textbox to database with oledb VFP in VB.NET
親愛的程序員,
如果我使用手冊,數據庫會立即更新,但如果我使用文本框,數據庫不會更新,只會出現更新消息框。 代碼或任何其他最佳建議有問題嗎? 我正在使用視覺工作室 2015
謝謝
Dim pathDBF As String = Application.StartupPath()
Using cn = New System.Data.OleDb.OleDbConnection($"Provider=VFPOLEDB;Data Source={pathDBF}")
Dim tableDBF = "RSD, GSD, GPD, FFG, RPD"
cn.Open()
For Each table In tableDBF.Split(","c)
'Using cmd = New OleDbCommand(String.Format("update {0} set Itemproduct=""Drink 1015 Full"" where Itemproduct==""Drink 1015""", tableDBF.Trim()), cn)
Using cmd = New OleDbCommand(String.Format("update {0} set Itemproduct=""?"" where Itemproduct==""?""", tableDBF.Trim()), cn)
cmd.Parameters.Add("Itemproduct", OleDbType.VarChar).Value = TextBoxnewitem.Text 'Drink 1015 Full
cmd.Parameters.Add("Itemproduct", OleDbType.VarChar).Value = TextBoxolditem.Text 'Drink 1015
cmd.ExecuteNonQuery()
End Using
MessageBox.Show("updated", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
Next
cn.Close()
End Using
當您使用參數 (?) 時,您不會在它們周圍使用任何引號,並且還要確保按照它們在 SQL 字符串中出現的順序指定它們(您做得正確):
Dim pathDBF As String = Application.StartupPath()
Using cn = New System.Data.OleDb.OleDbConnection($"Provider=VFPOLEDB;Data Source={pathDBF}")
Dim tableDBF = "RSD, GSD, GPD, FFG, RPD"
cn.Open()
For Each table In tableDBF.Split(","c)
'Using cmd = New OleDbCommand(String.Format("update {0} set Itemproduct=""Drink 1015 Full"" where Itemproduct==""Drink 1015""", tableDBF.Trim()), cn)
Using cmd = New OleDbCommand(String.Format("update {0} set Itemproduct=? where Itemproduct==?", tableDBF.Trim()), cn)
cmd.Parameters.Add("Itemproduct", OleDbType.VarChar).Value = TextBoxnewitem.Text 'Drink 1015 Full
cmd.Parameters.Add("Itemproduct", OleDbType.VarChar).Value = TextBoxolditem.Text 'Drink 1015
cmd.ExecuteNonQuery()
End Using
MessageBox.Show("updated", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
Next
cn.Close()
End Using
編輯:您還可以像這樣使它更具可讀性:
Dim sql As String = <sql>update {0}
set Itemproduct=?
where Itemproduct==?
</sql>
Using cmd = New OleDbCommand(String.Format(sql, tableDBF.Trim()), cn)
'...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.