Visual Basic .NET Access 数据库记录添加 [英] Visual Basic .NET Access Database Record Add
问题描述
我有数据库保存更改的问题:
I have problem with database save changes:
coon1.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & _
"magazyn.mdb"
sql = "INSERT INTO magazyn (ID_Towaru,Kod_Towaru,Nazwa_Towaru,Ilość_w_magazynie,ilość_minimalna,ALERT) VALUES ('" & jakiid & "','" & kodtowaru & "','" & nazwatowaru & "','" & iloscwmagazynie & "','" & iloscminimalna & "',0)"
Dim MyConnection As New OleDbConnection(conn)
Dim command1 As New OleDbCommand(sql, MyConnection)
command1.Connection.Open()
command1.ExecuteNonQuery()
MyConnection.Close()
我尝试将新记录添加到表 magazyn
,但是当使用 Access 打开数据库时,我在表中没有看到任何与 magazyn
相关的新记录.但是 ViewGrid
向我展示了这个新元素,直到我关闭并重新打开程序.
I try add new record to table magazyn
, but when opened database with Access then I didn't see any new record related to magazyn
in the table. But ViewGrid
shows me this new element until I close and re-open the program.
有人知道问题出在哪里吗?
Does someone know where the problem is?
推荐答案
始终使用参数而不是字符串连接.应严格遵守此规则
Always use parameters and not string concatenation. This rule should be followed categorically
sql = "INSERT INTO magazyn " +
"(Kod_Towaru,Nazwa_Towaru,Ilość_w_magazynie,ilość_minimalna,ALERT) " +
"VALUES (?, ?, ?, ?,0)"
Using MyConnection As New OleDbConnection(conn)
Using command1 As New OleDbCommand(sql, MyConnection)
command1.Connection.Open()
command1.Parameters.AddWithValue("@Kod", kodtowaru)
command1.Parameters.AddWithValue("@naz", nazwatowaru)
command1.Parameters.AddWithValue("@ilo", iloscwmagazynie)
command1.Parameters.AddWithValue("@mini", iloscminimalna)
command1.ExecuteNonQuery()
End Using
End Using
当然,这要求用作参数值的变量具有正确的数据类型.
This, of course, requires that the variables used as value for the parameters are of the correct datatype.
这篇关于Visual Basic .NET Access 数据库记录添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!