查询值和目标字段的数量不同。 [英] Number of query values and destination fields are not the same.

查看:80
本文介绍了查询值和目标字段的数量不同。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 an INSERT 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 use using 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 to OleDbCommand.


这篇关于查询值和目标字段的数量不同。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆