【问题标题】:incorrect syntax near the keyword 'Values'关键字“值”附近的语法不正确
【发布时间】:2013-05-21 22:02:06
【问题描述】:

这是我的代码。我想将这些值保存到数据库中。发生错误,

关键字值附近的语法不正确

foreach (GridViewRow gvr in GridView1.Rows)
{
string strcon1;
strcon1 = ConfigurationManager.ConnectionStrings["fwma_devConnectionString"].ConnectionString;
SqlConnection con1 = new SqlConnection(strcon1);
con1.Open();
SqlCommand com3 = new SqlCommand(strcon);
TextBox tb = (TextBox)gvr.FindControl("TextBox2");//value
string txt = tb.Text;

Label propertylabel = (Label)gvr.FindControl("Label4");//id-property

com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,Values) values(" + propertylabel.Text + "," + B_id.Text + "," + tb.Text + " ), con1";
com3.Connection = con1;
com3.ExecuteNonQuery();
con1.Close();

【问题讨论】:

  • 插入列列表中的括号值,即,[Values])
  • 文本属性周围不应该有 ' 吗?例如。 ('" + propertylabel.Text + "',")
  • @alexw 哦,很好,他错过了所有列。
  • @alexw:已更正,但发生了同样的错误
  • @ASAWYER & ALEXW :THANKU.. :)

标签: asp.net


【解决方案1】:

使用这个

com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,Values) values('" + propertylabel.Text + "','" + B_id.Text + "','" + tb.Text + "')";

而不是

com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,Values) values(" + propertylabel.Text + "," + B_id.Text + "," + tb.Text + " ), con1";

【讨论】:

  • 现在试试,如果你的列是字符串类型
  • 什么错误?能否在 SQL Query 中运行 SQL 命令,找出确切的问题。
【解决方案2】:

这条线不应该是这样的吗?

        com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,Values) values(" + propertylabel.Text + "," + B_id.Text + "," + tb.Text + ")";

请使用命令参数:

When should "SqlDbType" and "size" be used when adding SqlCommand Parameters?

【讨论】:

    【解决方案3】:

    如果您使用的是reserved keywords,则应指定带引号或括号的分隔标识符。

    使用括号的示例

    com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,[Values]) values(" + propertylabel.Text + "," + B_id.Text + "," + tb.Text + " ), con1";
    

    【讨论】:

      猜你喜欢
      • 2016-06-08
      • 2013-12-16
      • 2021-07-24
      • 2017-11-22
      • 2018-06-16
      • 2013-10-29
      • 2014-06-22
      • 1970-01-01
      • 2013-05-04
      相关资源
      最近更新 更多