帮助插入命令 [英] help with insert command
问题描述
我正在制作一个按钮,当点击它时,它将在访问时更新或插入数据库取决于访问表上存在的数据。当命令可以执行或成功时,将出现一个消息框。出现插入命令的消息框,没有显示错误但没有数据添加到表中。
i'm making a button that when get clicked it will update or insert database on access depend on data exist on access table.on my command when the command can be execute or success a messagebox will appear. the messagebox for insert command appear, no error shown but no data added to the table.
Sub ganti()
sql = "update suplist set [code]=?, [supplier's name]=?, [address]=?, [city]=?, [phone]=?, [fax]=?, [cellphone]=?, [information]=? where code=?"
kns.Open()
da.UpdateCommand = New OleDbCommand(sql, kns)
da.UpdateCommand.Parameters.AddWithValue("@p1", TextBox1.Text)
da.UpdateCommand.Parameters.AddWithValue("@p2", TextBox2.Text)
da.UpdateCommand.Parameters.AddWithValue("@p3", TextBox3.Text)
da.UpdateCommand.Parameters.AddWithValue("@p4", TextBox6.Text)
da.UpdateCommand.Parameters.AddWithValue("@p5", val(TextBox4.Text))
da.UpdateCommand.Parameters.AddWithValue("@p6", val(TextBox5.Text))
da.UpdateCommand.Parameters.AddWithValue("@p7", val(TextBox7.Text))
da.UpdateCommand.Parameters.AddWithValue("@p8", TextBox8.Text)
da.UpdateCommand.Parameters.AddWithValue("@p9", TextBox1.Text)
da.UpdateCommand.ExecuteNonQuery()
kns.Close()
End Sub
Sub tambah()
sql = "insert into [suplist] ([code],[supplier's name],[address],[city],[phone],[fax],[cellphone],[information] values (?,?,?,?,?,?,?,?)"
kns.Open()
da.InsertCommand = New OleDbCommand(sql, kns)
da.InsertCommand.Parameters.AddWithValue("@p1", TextBox1.Text)
da.InsertCommand.Parameters.AddWithValue("@p2", TextBox2.Text)
da.InsertCommand.Parameters.AddWithValue("@p3", TextBox3.Text)
da.InsertCommand.Parameters.AddWithValue("@p4", TextBox6.Text)
da.InsertCommand.Parameters.AddWithValue("@p5", Val(TextBox4.Text))
da.InsertCommand.Parameters.AddWithValue("@p6", Val(TextBox5.Text))
da.InsertCommand.Parameters.AddWithValue("@p7", Val(TextBox7.Text))
da.InsertCommand.Parameters.AddWithValue("@p8", TextBox8.Text)
kns.Close()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
carikode()
If ds.Tables("suplist").Rows.Count = 0 Then
tambah()
catat1()
MessageBox.Show("Supplier's Data Created", "Notice", MessageBoxButtons.OK)
semua()
TextBox1.Focus()
For Each c In GroupBox1.Controls.OfType(Of TextBox)()
c.Clear()
Next
ElseIf ds.Tables("suplist").Rows.Count = 1 Then
If MessageBox.Show("Are You Sure To Edit The Data?", "Warning", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes = True Then
ganti()
catat2()
MessageBox.Show("Supplier's Data Edited", "Notice", MessageBoxButtons.OK)
semua()
TextBox1.Focus()
For Each c In GroupBox1.Controls.OfType(Of TextBox)()
c.Clear()
Next
End If
End If
End Sub
i使用val因为表列是数字。任何人都可以帮助我。我尝试了另一种形式,另一个表,但仍然在相同的数据库上插入不同的数据,但具有相同的格式或类型的命令。有用。我已经尝试创建新表并删除了这个表,但它仍然无法正常工作。
i'm using val because the table column is on number. anyone can help me. i tried in another form, another table but still on same database with different data to insert but have same format or type of command. it works. i have tried make new table and deleted this table but it still not working.
推荐答案
你永远不会真正执行INSERT语句。
You're never actually executing the INSERT statement.
你错过了一个右括号)
。我发现格式化这样的语句有助于避免这样的错误。
You're missing a right-parenthesis)
. I find that formatting such statements helps to avoid such mistakes.
sql =
@"
insert into [suplist]
( [code]
, [supplier's name]
, [address]
, [city]
, [phone]
, [fax]
, [cellphone]
, [information]
values
( ?
, ?
, ?
, ?
, ?
, ?
, ?
, ?
)
"
这篇关于帮助插入命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!