我需要帮助:我正在尝试使用网页作为界面向数据库插入多个值。当我尝试保存时,我收到了错误
"关键字'值'附近的语法不正确。"
以下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 = "";
}
}
答案 0 :(得分:2)
您不能像尝试使用INSERT statement那样执行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注入。
答案 1 :(得分:0)
你有两次“价值”。你能试试吗?
“插入 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);