【问题标题】:Unable to insert mutiple values to database [closed]无法将多个值插入数据库[关闭]
【发布时间】:2016-01-27 15:00:11
【问题描述】:

我需要这方面的帮助:我正在尝试使用网页作为界面将多个值插入数据库。当我尝试保存时出现错误

“关键字 'values' 附近的语法不正确。”

下面的C#代码供大家参考。

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=INHRPSM1D7C;Initial Catalog=Controller_Forecast;Integrated Security=True;");

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void refress()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        TextBox7.Text = "";
        TextBox8.Text = "";
        TextBox9.Text = "";
        TextBox10.Text = "";
        TextBox11.Text = "";
        TextBox12.Text = "";
        DropDownList1.Text = "";
        DropDownList2.Text = "";
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("insert into Controller_Forecast(C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13) values('" +
            TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + 
            "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text +
            "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text +
            "','" + TextBox10.Text + "','" + TextBox11.Text + "','" + TextBox12.Text +
            "','" + DropDownList1.Text + "','" + DropDownList2.Text + "','" +
            DropDownList3.Text + "'),values('" + TextBox13.Text + "','" +
            TextBox14.Text + "','" + TextBox15.Text + "','" +
            TextBox16.Text + "','" + TextBox17.Text + "','" +
            TextBox18.Text + "','" + TextBox19.Text + "','" +
            TextBox20.Text + "','" + TextBox21.Text + "','" +
            TextBox22.Text + "','" + TextBox23.Text + "','" +
            TextBox24.Text + "','" + DropDownList4.Text + "','" +
            DropDownList5.Text + "','" + DropDownList6.Text + "')",con);

        cmd.CommandType = CommandType.Text;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            Literal1.Text = "Data Updated Successfully!!!";
            con.Close();
            refress();
        }
        catch (Exception ex)
        {
            Literal1.Text = ex.Message;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        refress();
        Literal1.Text = "";
    }
}

【问题讨论】:

  • 您使用连接创建的这个巨大的命令字符串有问题,可能缺少/多余的逗号或单引号。使用调试器并检查此命令在连接后的样子。甚至更好 - 根本不要使用连接,因为它会导致 sql 注入漏洞,而是使用参数化查询。
  • 我的系统中没有安装调试器,你可以修改查询并粘贴它
  • 你调用了两次values(...),这是无效的SQL语法。
  • 哦,男孩,little Bobby Tables 将与这个进行实地考察。
  • 查询中有两次 VALUES()。确保 VALUES() 子句中的数据项数量与您在开头指定的字段名称数量相匹配。

标签: c# asp.net linq


【解决方案1】:

您不能像尝试使用INSERT statement 那样执行两组值。您的有效行为:

INSERT INTO Controller_Forecast(C1,C2...)
VALUES(...loads of values...)
VALUES(...Loads of more values...)

这是无效的。要插入 2 组数据,这就是您想要执行的操作,您可以执行 2 INSERT INTO 语句或使用逗号拆分值列表,如下所示:

多个插入语句

INSERT INTO Controller_Forecast(C1,C2...)
VALUES(...your first set of values...)
INSERT INTO Controller_Forecast(C1,C2...)
VALUES(...your second set of values...)

用逗号分隔

INSERT INTO Controller_Forecast(C1,C2...)
VALUES(...your first set of values...), (...second set of values...)

但是,这似乎效率低下,您最好在存储过程中编写它并参数化您的值以避免 sql 注入。

【讨论】:

    【解决方案2】:

    你有两次“价值观”。你可以试试这个吗?

    "插入 Controller_Forecast(C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13) 值('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + TextBox11.Text + "','" + TextBox12.Text + "','" + DropDownList1.Text + "','" + DropDownList2.Text + "','" + DropDownList3.Text + "')",con);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2016-04-05
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      相关资源
      最近更新 更多