我正在尝试插入sql数据库但是我一直收到错误
以下是提到的查询:
str2 = "Insert into [Snaps](Loc_city,Loc_state,Loc_country,Edu_Hist1,Work_Hist1) values (Loc_city= ' " + soap.data[i1].current_location.city.ToString() + "', Loc_state='" + soap.data[i1].current_location.state.ToString() + "', Loc_country='" + soap.data[i1].current_location.country.ToString() + "',Edu_Hist1='" + soap.data[i1].education_history[0].name.ToString() + "', Work_Hist1 ='" + soap.data[i1].education_history[0].school_type.ToString()+"')";
cmd = new SqlCommand(str2,con);
cmd.ExecuteNonQuery();
请帮忙
答案 0 :(得分:6)
您的值列表中不需要'Loc_city = ...'。它应该只是:
str2 = "Insert into [Snaps](Loc_city,Loc_state,Loc_country,Edu_Hist1,Work_Hist1) values ('" + soap.data[i1].current_location.city.ToString() + "', '" + soap.data[i1].current_location.state.ToString() + "', '" + soap.data[i1].current_location.country.ToString() + "', '" + soap.data[i1].education_history[0].name.ToString() + "', '" + soap.data[i1].education_history[0].school_type.ToString()+"')";
答案 1 :(得分:3)
您不需要值Loc_city=
。
只需传递以逗号分隔的值
答案 2 :(得分:3)
VALUES子句应该只包含实际数据,插入的顺序和列已经在INSERT语句中指定。
示例:
INSERT INTO testTable(colA,colB,colC) VALUES('VarcharCol',22,'VarcharColC')
答案 3 :(得分:2)
插入值时删除字段的名称: 从:
values (Loc_city= ' " + soap.data[i1].current_location.city.ToString() + "', Loc_state='" + soap.data[i1].current_location.state.ToString() + "', Loc_country='" + soap.data[i1].current_location.country.ToString() + "',Edu_Hist1='" + soap.data[i1].education_history[0].name.ToString() + "', Work_Hist1 ='" + soap.data[i1].education_history[0].school_type.ToString()+"')";
要:
values ('" + soap.data[i1].current_location.city.ToString() + "', '" + soap.data[i1].current_location.state.ToString() + "', '" + soap.data[i1].current_location.country.ToString() + "', '" + soap.data[i1].education_history[0].name.ToString() + "', '" + soap.data[i1].education_history[0].school_type.ToString()+"')";