简体   繁体   English

如何将数据插入SQL Server数据库

[英]How to insert Data into sql server database

I'm trying to insert data into my SQL Server 2014 database, but I get an error 我正在尝试将数据插入我的SQL Server 2014数据库中,但出现错误

Incorrect syntax near ')'. ')'附近的语法不正确。

My table matches the types of data I put in, for example, I'm put an int into a int . 我的表与我放入的数据类型匹配,例如,我将int放入int

Here is my code: 这是我的代码:

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();
        }  
    }
}

So please help, thanks 所以请帮忙,谢谢

Your insert syntax is not correct. 您的插入语法不正确。 you have not given column names also keyword "Values" is missing in your query 您尚未提供列名,查询中也缺少关键字“值”

Your parameter names can not have brackets or spaces in them in SQL Server. 在SQL Server中,参数名称中不能包含方括号或空格。 So rename them all to @SignNumber, @redquantity, @bluequantity... etc. 因此,将它们全部重命名为@ SignNumber,@ redquantity,@ bluequantity ...等。

Try this code. 试试这个代码。 you will get your desired result. 您将获得理想的结果。 The issue that you were getting in your code is because parameters are not supposed to have any spaces or brackets or any characters that makes the parameter names not well formed. 您在代码中遇到的问题是,因为参数不应包含任何空格或方括号或任何使参数名称格式不正确的字符。 A parameter name must begin with "@" character, and should follow the rules for object identifiers. 参数名称必须以“ @”字符开头,并且应遵循对象标识符的规则。 Check the link for further details. 检查链接以获取更多详细信息。

    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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM