查询值和目标字段的数量不同。 [英] Number of query values and destination fields are not the same.
问题描述
Dim con As 新 OleDbConnection
Dim cmd1 作为 新 OleDbCommand
Dim ddt 作为 新 OleDbDataAdapter
Dim dtd As New DataSet
con.ConnectionString =( Provider = Microsoft.Jet.OLEDB.4.0;数据源= D:\ Ticket.mdb)
cmd1.Connection = con
con.Open()
cmd1.CommandText = INSERT INTO regis VALUES('& TextBox1.Text& ','& TextBox2.Text& ','& TextBox3.Text& ')
cmd1.ExecuteNonQuery()
MessageBox.Show(< span class =code-string> 成功注册)
con.Close()
我尝试了什么:
请帮忙...我试过检查但它似乎很好知道问题是什么
永远不要依赖INSERT
声明。而是定义您要使用的所有列。所以语法应该是
INSERT INTO TableName
(ColumnName,ColumnName,...)
VALUES
(价值,价值,......)
另一件事是你应该总是在查询中使用参数。永远不要将值直接连接到SQL语句。欲了解更多信息,请查看 OleDbParameter Class(System .Data.OleDb) [ ^ ]
另外你应该使用使用
声明以确保正确处理对象。尽管文章是关于SqlCommands的,但请查看正确执行数据库操作 [ ^ ]同样的原则适用于的OleDbCommand
Dim con As New OleDbConnection
Dim cmd1 As New OleDbCommand
Dim ddt As New OleDbDataAdapter
Dim dtd As New DataSet
con.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Ticket.mdb")
cmd1.Connection = con
con.Open()
cmd1.CommandText = "INSERT INTO regis VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "' ,'" & TextBox3.Text & "' )"
cmd1.ExecuteNonQuery()
MessageBox.Show("Succesfully Registered")
con.Close()
What I have tried:
Please help...i tried checking but its seems good cant know whats the problem
Never rely on the implicit column amount or order in anINSERT
statement. Instead, define all the columns you're going to use. So the syntax should be
INSERT INTO TableName (ColumnName, ColumnName, ...) VALUES (Value, Value, ...)
And the other thing is that you should always use parameters with queries. Never concatenate values directly to SQL statements. For more information have a look at OleDbParameter Class (System.Data.OleDb)[^]
Also you should useusing
statement to ensure that objects are properly disposed. Even though the article is written about SqlCommands, have a look at Properly executing database operations[^] The same principles apply toOleDbCommand
.
这篇关于查询值和目标字段的数量不同。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!