无法将多个值插入数据库
本文关键字:插入 数据库 | 更新日期: 2023-09-27 18:32:11
我需要这方面的帮助:我正在尝试使用网页作为界面将多个值插入数据库。当我尝试保存时,出现错误
"关键字'值'附近的语法不正确。"
下面是 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 = "";
}
}
你不能像尝试使用 INSERT 语句那样做 2 组值。您的有效工作:
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 注入。
你有两次"值"。你能试试这个吗?
"插入 Controller_Forecast(C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13) 值('" + 文本框1.文本 + ","+ 文本框2.文本 + "," + 文本框3.文本 + "'"," + 文本框4.文本 + ","" + 文本框5.文本 + "," + 文本框6.文本 + "'"," + 文本框7.文本 + ","" + 文本框8.文本 + "," + 文本框9.文本 + "," + 文本框10.文本 + "," + 文本框11.文本 + "," + 文本框12.文本 + "','" + 下拉列表1.文本 + ","" + 下拉列表2.文本 + ","+ 下拉列表3.文本 + "')",con);