请我在此代码中有问题(错误消息:插入错误列名称或提供的值数不匹配) [英] please i have problem in this code (error message: insert error column name or number of supplied values does not match)
问题描述
此代码和表单的形状
this code here and shape of form
con = new SqlConnection(str);
da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand("INSERT INTO customers VALUES (@insurance_type,@branch_name,@name,@address,@insurance_cost,@record_date)", con);
da.InsertCommand.Parameters.Add("@insurance_type", SqlDbType.NVarChar).Value =
comboBox2.SelectedItem ;
da.InsertCommand.Parameters.Add("@branch_name", SqlDbType.NVarChar).Value =
comboBox1.SelectedItem;
da.InsertCommand.Parameters.Add("@name", SqlDbType.Money).Value =
textBox1.Text;
da.InsertCommand.Parameters.Add("@address", SqlDbType.NVarChar).Value =
textbox2.Value;
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();
推荐答案
确保发送正确的值和类型
Make sure you are sending in the proper values and types
da.InsertCommand.Parameters.Add("@name", SqlDbType.Money).Value =
textBox1.Text;
这是期望的浮动,但您正在发送文本.如果用户输入"ABC"会怎样?它不能转换为浮点数. DateTime列也是如此.
This is expecting a float but you are sending text. What happens if the user enters "ABC"? It can''t be converted to a float. The same goes for the DateTime column.
da.InsertCommand.Parameters.Add("@insurance_type", SqlDbType.NVarChar).Value =
comboBox2.SelectedItem ;
在这里,您需要一个字符串,但是SelectedItem是ListItem. ListItem将返回使用ToString方法将其转换为字符串,但这真的是您想要的吗?
Here you are expecting a string but SelectedItem is ListItem. The ListItem will return use the ToString method to convert it to a string, but is that really want you want?
我认为您必须在insert语句中指定列. >
I think you have to specify the columns in your insert statement.
Insert Into [Table Name] (Column1, Column2, Column3) Values (Val1, Val2, Val3)
您必须为表中的每一列提供一个值,并且必须按照正确的顺序来匹配表.
我的意思是;您必须在两边都匹配列数.如果有5列,则必须有5个参数.因此,括号中的列数必须与右侧的值数匹配.列1和值1的类型必须匹配,检查类型是否匹配.
好的lcuk
OI
You HAVE to provide a value for every column in the table and it has to be in the proper order to match the table.
What I mean by this is; you have to match the column count in both sides. If you have 5 columns, you have to have 5 parameters. So the column count in the paranthesis must match the value count in the right hand side. The types for the column 1 and value 1 has to match check if the types match.
Good lcuk
OI
这篇关于请我在此代码中有问题(错误消息:插入错误列名称或提供的值数不匹配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!