简体   繁体   中英

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

Incorrect syntax near ')'.

My table matches the types of data I put in, for example, I'm put an int into a 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. So rename them all to @SignNumber, @redquantity, @bluequantity... etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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