且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PL很好,有人在此保存按钮代码中遇到了问题(错误消息:插入错误:列名或提供的值数不匹配)

更新时间:2023-01-12 20:53:28

表中有几列?如果不是四个,并且它们的排列顺序不正确,即使将额外"列标记为允许的空值",也会出现错误.尝试重新处理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