我遇到问题,当我运行我的代码时,会出现“查询值和目标字段数不相同的错误。”
plz建议我这个。
“代码”
protected void btn_Save_Click(object sender, EventArgs e)
{
string str = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Geeta/Desktop/eTimeTrackLite1.mdb;Persist Security Info=False;");
OleDbConnection conn = new OleDbConnection(str);
conn.Open();
string query = "insert into Employees (EmployeeName,EmployeeCode,DeviceCode,Company,Department,Designation,Grade,Team,Location,EmploymentType,Category,HolidayGroup,ShiftGroup,ShiftRoster,Dateofjoining,Dateofconfirmation,Status,DateofResigning,Sex) values ('" + txt_empname.Text + "','" + txt_code.Text + "', '" + txt_dcode.Text + "', '" + Convert.ToString(dp_company.SelectedItem)+ "', '" + Convert.ToString(dp_department.SelectedItem) + "', '"+Convert.ToString(dp_designation.SelectedItem)+"', '"+Convert.ToString(dp_grade.SelectedItem)+"', '"+Convert.ToString(dp_team.SelectedItem)+"', '"+Convert.ToString(dp_location.SelectedItem)+"', '"+Convert.ToString(dp_emptype.SelectedItem)+"', '"+Convert.ToString(dp_category.SelectedItem)+"', '"+Convert.ToString(dp_holigroup.SelectedItem)+"', '"+Convert.ToString(dp_shiftgroup.SelectedItem)+"', '"+Convert.ToString(dp_shiftroster.SelectedItem)+"', '"+Convert.ToString(dp_day.SelectedItem)+"', '"+Convert.ToString(dp_month.SelectedItem)+"', '"+Convert.ToString(dp_year.SelectedItem)+"', '"+Convert.ToString(dp_cday.SelectedItem)+"', '"+Convert.ToString(dp_cmonth.SelectedItem)+"', '"+Convert.ToString(dp_cyear.SelectedItem)+"', '"+Convert.ToString(dp_status.SelectedItem)+"', '"+Convert.ToString(dp_rday.SelectedItem)+"', '"+Convert.ToString(dp_rmonth.SelectedItem)+"', '"+Convert.ToString(dp_ryear.SelectedItem)+"', '"+Convert.ToString(rdbtn_male.Checked)+"', '"+Convert.ToString(rdbtn_female.Checked)+"')";
OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.ExecuteNonQuery();
conn.Close();
BindGridData();
}
“谢谢”
答案 0 :(得分:0)
此查询的问题在于您告诉查询需要19个插入参数(EmployeeName,EmployeeCode,DeviceCode,Company,Department,Designation,Grade,Team,Location,EmploymentType,Category,HolidayGroup,ShiftGroup,ShiftRoster,Dateofjoining ,Dateofconfirmation,状态,DateofResigning,性别)
但是你试图插入更多(26) - 如果你计算你试图插入的值,你会发现它们不匹配。
因此,您可以删除查询后半部分中的额外值,或添加其他插入参数。例如,您似乎尝试多次插入“年”,“月”和“日”的值,但您只有一个日期字段。您应该将它们连接在一起并转换为日期,以使插入工作正常。