如何将数据插入 sql 服务器数据库
How to insert Data into sql server database
我正在尝试将数据插入我的 SQL Server 2014 数据库,但出现错误
Incorrect syntax near ')'.
我的table匹配我输入的数据类型,比如我把一个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();
}
}
}
所以请帮忙,谢谢
您的参数名称在 SQL 服务器中不能有括号或空格。所以将它们全部重命名为@SignNumber、@redquantity、@bluequantity...等等
您的插入语法不正确。您没有给出列名,您的查询中也缺少关键字 "Values"
试试这个代码。你会得到你想要的结果。您在代码中遇到的问题是因为参数不应该有任何空格或括号或任何使参数名称格式不正确的字符。参数名称必须以“@”字符开头,并且应遵循对象标识符的规则。查看 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;
}
我正在尝试将数据插入我的 SQL Server 2014 数据库,但出现错误
Incorrect syntax near ')'.
我的table匹配我输入的数据类型,比如我把一个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();
}
}
}
所以请帮忙,谢谢
您的参数名称在 SQL 服务器中不能有括号或空格。所以将它们全部重命名为@SignNumber、@redquantity、@bluequantity...等等
您的插入语法不正确。您没有给出列名,您的查询中也缺少关键字 "Values"
试试这个代码。你会得到你想要的结果。您在代码中遇到的问题是因为参数不应该有任何空格或括号或任何使参数名称格式不正确的字符。参数名称必须以“@”字符开头,并且应遵循对象标识符的规则。查看 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;
}