插入查询时出错

时间:2012-02-20 15:54:00

标签: c# asp.net sql-server sql-server-2008 sql-server-2005

我正在尝试插入sql数据库但是我一直收到错误

enter image description here

以下是提到的查询:

 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();

请帮忙

4 个答案:

答案 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()+"')";

考虑使用SqlParameter with your SqlCommandstring.format()来清理查询。