PL很好,有人在此保存按钮代码中遇到了问题(错误消息:插入错误:列名或提供的值数不匹配) [英] PL nice someone i have aproblem in this save button code (erro message:insert error :column name or number of supplied values does not match)

查看:66
本文介绍了PL很好,有人在此保存按钮代码中遇到了问题(错误消息:插入错误:列名或提供的值数不匹配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码

this is code

con = new SqlConnection(str);
            da = new SqlDataAdapter();
            da.InsertCommand = new SqlCommand("INSERT INTO customers VALUES (@name,@address,@insurance_cost,@record_date)", con);
            da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar).Value = textBox1.Text;
            da.InsertCommand.Parameters.Add("@address", SqlDbType.NVarChar).Value = textBox2.Text;
            da.InsertCommand.Parameters.Add("@insurance_cost", SqlDbType.Money).Value = textBox3.Text;
            da.InsertCommand.Parameters.Add("@record_date", SqlDbType.DateTime).Value = dateTimePicker1.Value;
            con.Open();
            da.InsertCommand.ExecuteNonQuery();
            con.Close();

推荐答案

表中有几列?如果不是四个,并且它们的排列顺序不正确,即使将额外"列标记为允许的空值",也会出现错误.尝试重新处理insert语句,以将其设置的顺序列出这些字段:
How many columns do you have in the table? If it isn''t four, and they aren''t in the right order, you will get an error, even if the "extra" columns are marked as "nulls allowed". Try reworking the insert statement to list teh fields in teh order they will be set:
INSERT INTO customers (name, address, insurance_cost, record_date) VALUES (@name,@address,@insurance_cost,@record_date)



顺便说一句:您不应该使用Add方法-它在几年前就已贬值,并且在将来的版本中可能不受支持.使用AddWithValue代替:



BTW: You shouldn''t use the Add method - it was depreciated years ago, and may not be supported in future versions. Use AddWithValue instead:

da.InsertCommand = new SqlCommand("INSERT INTO customers (name, address, insurance_cost, record_date) VALUES (@name,@address,@insurance_cost,@record_date)", con);
da.InsertCommand.Parameters.AddWithValue("@name", textBox1.Text);
da.InsertCommand.Parameters.AddWithValue("@address", textBox2.Text);
da.InsertCommand.Parameters.AddWithValue("@insurance_cost", textBox3.Text);
da.InsertCommand.Parameters.AddWithValue("@record_date", dateTimePicker1.Value);


请重新检查查询语法是否正确.正确的语法如下:
Please recheck your query it''s sytax is wrong.The correct Syntax is as follows :
da.InsertCommand = new SqlCommand("INSERT INTO customers (name,address,insurance_cost,record_date) VALUES (@name,@address,@insurance_cost,@record_date)", con);
            da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar).Value = textBox1.Text;
            da.InsertCommand.Parameters.Add("@address", SqlDbType.NVarChar).Value = textBox2.Text;
            da.InsertCommand.Parameters.Add("@insurance_cost", SqlDbType.Money).Value = textBox3.Text;
            da.InsertCommand.Parameters.Add("@record_date", SqlDbType.DateTime).Value = dateTimePicker1.Value;
            con.Open();
            da.InsertCommand.ExecuteNonQuery();
            con.Close();


还要检查表中存在的列数以及在查询中写入的列数及其名称是否正确. :)

-MKB


Also checkout the no of columns present in your table and no of columns you wrote in the query and their Names are proper or not. :)

- MKB


这篇关于PL很好,有人在此保存按钮代码中遇到了问题(错误消息:插入错误:列名或提供的值数不匹配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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