查询值和目标字段的数量不同./ INSERT INTO语句中的语法错误 [英] Number of query values and destination fields are not the same./Syntax error in INSERT INTO statement

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

问题描述

当Iam插入表格时......我收到了一些错误。

尝试了很多方法....但它没有用......请帮忙!

我为另一个表做了相同的代码并且它被添加...我找不到错误。



对于以下代码,它给出了查询值和目标的数量字段不一样

 ConnectionToDB(); 
string sql = 插入TW100NEL VALUES (' + pl + ',' + desc + ', + grp + + qty + ,' + unit + ');
comm.CommandType = CommandType.Text;
comm.CommandText = sql;
comm.Connection = conn;
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show( 已成功添加!);
conn.Close();









我写如下,它给出错误:INSERT INTO语句中的语法错误

  string  sql =   INSERT INTO TW100NEL(ComponentNumber,ObjectDescription,Group,Quantity,Unit)VALUES(@ pl,@ desc,@ grp, @数量,@单元); 
comm.Parameters.AddWithValue( @ pl,pl);
comm.Parameters.AddWithValue( @ desc,desc);
comm.Parameters.AddWithValue( @ grp,grp);
comm.Parameters.AddWithValue( @ qty,qty);
comm.Parameters.AddWithValue( @ unit,unit);
comm.CommandType = CommandType.Text;
comm.CommandText = sql;
comm.Connection = conn;
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show( 已成功添加!);
conn.Close();





我检查了所有行..这是正确的,无论我输入什么值到可编辑的行。

我正在处理accdb。

以下查询在accdb查询设计中运行良好:

插入TW100NEL VALUES('1234','zzzzzzz',4,2,'PC'); 





我做了不显示连接代码,因为它适用于其他sql。



提前谢谢。

解决方案

< blockquote> Group是SQL Server中的关键字。它必须用方括号包裹。





尝试下面的代码。



  INSERT   INTO  TW100NEL 
(ComponentNumber,ObjectDescription, [ Group ],Quantity,Unit) VALUES @ pl @ desc @ grp @ qty @ unit


如果是MySQL数据库,则需要将关键字包装在`字符中。 (ASCII = 0x60)



  INSERT   INTO  TW100NEL 
(ComponentNumber,ObjectDescription,`Group`,Quantity,Unit)
VALUES
@ pl @ desc @ grp @ qty @ unit

查看这个



 ConnectionToDB(); 
string sql = INSERT INTO table_name( ComponentNumber,ObjectDescription,[Group],Quantity,Unit)VALUES(' + pl + ',' + desc + ', + grp + + qty + , ' + unit + ');
comm.CommandType = CommandType.Text;
comm.CommandText = sql;
comm.Connection = conn;
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show( 已成功添加!);
conn.Close();


When Iam inserting into tables...i get some errors.
Tried many method....but it did not work...please help!
I did the same code for another table and it gets added... i can not find the error.

For Following code it gives "Number of query values and destination fields are not the same"

ConnectionToDB();
string sql = "INSERT INTO TW100NEL VALUES('"+pl+"','"+desc+"',"+grp+","+qty+",'"+unit+"')";
comm.CommandType = CommandType.Text;
comm.CommandText = sql;
comm.Connection = conn;
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show("Successfully Added!");
conn.Close();





When i write like following, it give error :"Syntax error in INSERT INTO statement"

string sql ="INSERT INTO TW100NEL(ComponentNumber,ObjectDescription,Group,Quantity,Unit) VALUES(@pl,@desc,@grp,@qty,@unit)";
comm.Parameters.AddWithValue("@pl", pl);
comm.Parameters.AddWithValue("@desc", desc);
comm.Parameters.AddWithValue("@grp", grp);
comm.Parameters.AddWithValue("@qty", qty);
comm.Parameters.AddWithValue("@unit", unit);
comm.CommandType = CommandType.Text;
comm.CommandText = sql;
comm.Connection = conn;
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show("Successfully Added!");
conn.Close();



I have checked all rows..it is correct and whatever value i am entering goes to rows which are editable.
I am working on accdb.
Following query works well in accdb query design :

INSERT INTO TW100NEL VALUES('1234','zzzzzzz',4,2,'PC');



I did not show code of connections because it is working well for other sql.

Thank you in advance.

解决方案

Group is a keyword in SQL server. It must be wrapped by square brackets.


Try below code.

INSERT INTO TW100NEL
(ComponentNumber,ObjectDescription,[Group],Quantity,Unit) VALUES(@pl,@desc,@grp,@qty,@unit)


If it is a MySQL database you need to wrap keywords in a ` character. (ASCII = 0x60)

INSERT INTO TW100NEL
    (ComponentNumber, ObjectDescription, `Group`, Quantity, Unit) 
VALUES 
    (@pl, @desc, @grp, @qty, @unit)


Check with this

ConnectionToDB();
string sql = "INSERT INTO table_name (ComponentNumber,ObjectDescription,[Group],Quantity,Unit) VALUES('"+pl+"','"+desc+"',"+grp+","+qty+",'"+unit+"')";
comm.CommandType = CommandType.Text;
comm.CommandText = sql;
comm.Connection = conn;
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show("Successfully Added!");
conn.Close();


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

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