【问题标题】:Error in Insert query插入查询中的错误
【发布时间】:2012-02-20 15:54:00
【问题描述】:

我正在尝试插入 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();

请帮忙

【问题讨论】:

  • 您要更新现有字段还是在表中插入新字段?你的 sql 完全错误。
  • 如您所见,我插入的不是更新
  • 布莱恩的回答就是我所说的。您只需要在更新语句中使用 'Loc_city=...'。

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


【解决方案1】:

您的值列表中不需要“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()+"')";

【讨论】:

    【解决方案2】:

    您不需要值上的Loc_city=。 只传递逗号分隔的值

    【讨论】:

      【解决方案3】:

      VALUES 子句应该只包含实际数据,其插入的顺序和列已在 INSERT 语句中指定。

      示例:

      插入测试表(colA、colB、colC) 值('VarcharCol',22,'VarcharColC')

      【讨论】:

        【解决方案4】:

        在插入值时删除字段名称: 来自:

        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 SqlCommand 或仅使用string.format() 来清理查询。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-12-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-11
          相关资源
          最近更新 更多