我正在尝试将数据插入到我的SQL Server 2014数据库中,但是我收到错误
')'附近的语法不正确。
我的表格与我输入的数据类型相匹配,例如,我将int
放入int
。
这是我的代码:
string ip = Request.UserHostAddress;
string name = "Jesus said: Love your Enemies (V.S.)";
int blueq = Convert.ToInt32(TextBox1.Text);
int redq = Convert.ToInt32(TextBox2.Text);
int whiteq = Convert.ToInt32(TextBox3.Text);
int blackq = Convert.ToInt32(TextBox4.Text);
int whiteqr = Convert.ToInt32(TextBox9.Text);
int redqr = Convert.ToInt32(TextBox10.Text);
int sn = 600;
int price = total_jslye;
string size;
if (RadioButton1.Checked == false)
{
size = "11x35";
}
else
size = "18x50";
try
{
string conn = System.Configuration.ConfigurationManager.ConnectionStrings["SQLCS"].ConnectionString;
var cmd = "INSERT INTO cartsigns (@SignNumber, @redquantity, @bluequantity, @whitequantity, @blackquantity, @whitereflectivequantity, @redreflectivequantity, @size, @SignName, @ipaddress, @price)";
using (SqlConnection com = new SqlConnection(conn))
{
using (SqlCommand cmds = new SqlCommand(cmd, com))
{
cmds.Parameters.AddWithValue("@SignNumber", sn);
cmds.Parameters.AddWithValue("@redquantity", redq);
cmds.Parameters.AddWithValue("@bluequantity", blueq);
cmds.Parameters.AddWithValue("@whitequantity", whiteq);
cmds.Parameters.AddWithValue("@blackquantity", blackq);
cmds.Parameters.AddWithValue("@whitereflectivequantity", whiteqr);
cmds.Parameters.AddWithValue("@redreflectivequantity", redqr);
cmds.Parameters.AddWithValue("@size", size);
cmds.Parameters.AddWithValue("@SignName", name);
cmds.Parameters.AddWithValue("@ipaddress", ip);
cmds.Parameters.AddWithValue("@price", price);
com.Open();
cmds.ExecuteNonQuery();
}
}
}
请帮助,谢谢
答案 0 :(得分:5)
您的插入语法不正确。你还没有给出列名,你的查询中也缺少关键字“值”
答案 1 :(得分:2)
您的参数名称在SQL Server中不能包含括号或空格。所以将它们全部重命名为@SignNumber,@ redquantity,@ bluequantity ......等。
答案 2 :(得分:2)
试试这段代码。你会得到你想要的结果。您在代码中遇到的问题是因为参数不应该包含任何空格或括号或任何使参数名称形式不正确的字符。参数名称必须以“@”字符开头,并且应遵循对象标识符的规则。查看link了解更多详情。
string ip = Request.UserHostAddress;
string name = "first sign";
int blueq = Convert.ToInt32(TextBox1.Text);
int redq = Convert.ToInt32(TextBox2.Text);
int whiteq = Convert.ToInt32(TextBox3.Text);
int blackq = Convert.ToInt32(TextBox4.Text);
int whiteqr = Convert.ToInt32(TextBox9.Text);
int redqr = Convert.ToInt32(TextBox10.Text);
int sn = 600;
int price = total_jslye;
string size;
if (RadioButton1.Checked == false)
{
size = "11x35";
}
else
size = "18x50";
try
{
string conn = System.Configuration.ConfigurationManager.ConnectionStrings["SQLCS"].ConnectionString;
var cmd = "INSERT INTO cartsigns ([Sign Number],[red quantity],[blue quantity],[white quantity],[black quantity],[white reflective quantity],[red reflective quantity],[size],[Sign Name],[ipaddress],[price]) values (@[Sign_Number],@[red_quantity],@[blue_quantity], @[white_quantity],@[black_quantity],@[white_reflective_quantity],@[red_reflective_quantity],@[size],@[Sign_Name],@[ipaddress],@[price])";
using (SqlConnection com = new SqlConnection(conn))
{
using (SqlCommand cmds = new SqlCommand(cmd, com))
{
cmds.Parameters.AddWithValue("@[Sign_Number]", sn);
cmds.Parameters.AddWithValue("@[red_quantity]", redq);
cmds.Parameters.AddWithValue("@[blue_quantity]", blueq);
cmds.Parameters.AddWithValue("@[white_quantity]", whiteq);
cmds.Parameters.AddWithValue("@[black_quantity]", blackq);
cmds.Parameters.AddWithValue("@[white_reflective_quantity]", whiteqr);
cmds.Parameters.AddWithValue("@[red_reflective_quantity]", redqr);
cmds.Parameters.AddWithValue("@[size]", size);
cmds.Parameters.AddWithValue("@[Sign_Name]", name);
cmds.Parameters.AddWithValue("@[ipaddress]", ip);
cmds.Parameters.AddWithValue("@[price]", price);
com.Open();
cmds.ExecuteNonQuery();
}
}
}
catch
{
throw;
}