简体   繁体   English

SQL C#附近的语法不正确

[英]Sql C# Incorrect syntax near

I tried to make a code to insert all my data from a grid into a table. 我试图编写代码以将所有数据从网格插入表中。 In the grid I display what I need, it's not the problem, or it does not give an error 在网格中,我显示了我所需要的,这不是问题,或者没有给出错误

Displays this error: 显示此错误:

System.Data.SqlClient.SqlException: Incorrect syntax near '{' System.Data.SqlClient.SqlException:“ {”附近的语法不正确

string StrQuery;
                try
                {
                    using (SqlConnection conn = new SqlConnection(stringcon))
                    {
                        using (SqlCommand comm = new SqlCommand())
                        {
                            comm.Connection = conn;
                            conn.Open();
                            for (int i = 1; i < bunifuCustomDataGrid2.Rows.Count; i++)
                            {
                           StrQuery = @"INSERT INTO concediati VALUES ("
                            + bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString() + ", "
                             + bunifuCustomDataGrid2.Rows[i].Cells["lastname"].ToString() + ", "
                             + bunifuCustomDataGrid2.Rows[i].Cells["CARS"].ToString() + ", "
                             + bunifuCustomDataGrid2.Rows[i].Cells["RENT"].ToString() + ", "
                            + bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].ToString() + ");";
                        comm.CommandText = StrQuery;
                        comm.ExecuteNonQuery();
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }

Updated with parameters. 更新了参数。

string StrQuery;
            try
            {
                using (SqlConnection conn = new SqlConnection(stringcon))
                {
                    using (SqlCommand comm = new SqlCommand())
                    {
                        comm.Connection = conn;
                        conn.Open();
                        for (int i = 0; i < bunifuCustomDataGrid2.Rows.Count; i++)
                        {


                            StrQuery = @"INSERT INTO concediati(nume,prenume,idcar,idrent,idclient) VALUES (@name,@lastname,@car,@rent,@client)";
                            comm.Parameters.AddWithValue("@name", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString()));
                            comm.Parameters.AddWithValue("@lastname", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["lastname"].ToString()));
                            comm.Parameters.AddWithValue("@car", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CARS"].ToString()));
                            comm.Parameters.AddWithValue("@rent", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["RENT"].ToString()));
                            comm.Parameters.AddWithValue("@client", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].ToString()));



                            comm.CommandText = StrQuery;
                            comm.ExecuteNonQuery();
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;


}

And now it gives a different error: 现在,它给出了另一个错误:

System.FormatException: 'Input string was not in a correct format.' System.FormatException:'输入字符串的格式不正确。

Pictures: capture1 capture25 capture25 capture25 capture5 图片: capture1 capture25 capture25 capture25 capture5

Table: 表:

CREATE TABLE [dbo].[concediati] (
    [Id]       INT          IDENTITY (1, 1) NOT NULL,
    [nume]     VARCHAR (50) NULL,
    [prenume]  VARCHAR (50) NULL,
    [idclient] INT          NULL,
    [idrent]   INT          NULL,
    [idcar]    INT          NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString()) gives you the overriden implementation of ToString method. bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString())为您提供ToString方法的替代实现。 That means you are not getting the actual values from above code. 这意味着您没有从上述代码中获得实际值。 You should use bunifuCustomDataGrid2.Rows[i].Cells["firstname"].Value instead. 您应该改用bunifuCustomDataGrid2.Rows[i].Cells["firstname"].Value

Please mark it as answered if it helps. 如果有帮助,请标记为已回答。

The documentation for INSERT shows a space between the table name and the list of columns, so it would be best to follow that. INSERT文档在表名和列列表之间显示一个空格,因此最好遵循该空格。

Also, you can create the parameters just once outside the loop and set their values in the loop (otherwise you would need to call .Clear() on the parameters and re-create them on every iteration): 另外,您可以在循环外创建一次参数,然后在循环中设置它们的值(否则,您需要在参数上调用.Clear(),并在每次迭代时重新创建它们):

string sql = @"INSERT INTO concediati (nume, prenume, idcar, idrent, idclient) VALUES (@name, @lastname, @car, @rent, @client)";
using (SqlConnection conn = new SqlConnection(stringcon))
{
    using (SqlCommand comm = new SqlCommand(sql, conn))
    {
        comm.Parameters.Add(new SqlParameter { ParameterName = "@name", SqlDbType = SqlDbType.VarChar, Size = 50 });
        comm.Parameters.Add(new SqlParameter { ParameterName = "@lastname", SqlDbType = SqlDbType.VarChar, Size = 50 });
        comm.Parameters.Add(new SqlParameter { ParameterName = "@car", SqlDbType = SqlDbType.Int });
        comm.Parameters.Add(new SqlParameter { ParameterName = "@rent", SqlDbType = SqlDbType.Int });
        comm.Parameters.Add(new SqlParameter { ParameterName = "@client", SqlDbType = SqlDbType.Int });

        conn.Open();

        for (int i = 0; i < bunifuCustomDataGrid2.Rows.Count; i++)
        {
            string firstName = Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["firstname"].Value);
            string lastName = Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["lastname"].Value);
            int car = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CARS"].Value);
            int rent = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["RENT"].Value);
            int client = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].Value);

            comm.Parameters["@name"].Value = firstName;
            comm.Parameters["@lastname"].Value = lastName;
            comm.Parameters["@car"].Value = car;
            comm.Parameters["@rent"].Value = rent;
            comm.Parameters["@client"].Value = client;

            comm.ExecuteNonQuery();

        }
    }
}

I checked your code and made this change. 我检查了您的代码并进行了更改。 You can use the following code. 您可以使用以下代码。

 string StrQuery;
        try
        {
            using (SqlConnection conn = new SqlConnection(stringcon))
            {

                    for (int i = 0; i < bunifuCustomDataGrid2.Rows.Count; i++)
                    {
                        SqlCommand comm = new SqlCommand();
                        comm.Connection = conn;
                        StrQuery = @"INSERT INTO concediati(nume,prenume,idcar,idrent,idclient) VALUES (@name,@lastname,@car,@rent,@client)";
                        comm.Parameters.AddWithValue("@name", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString()));
                        comm.Parameters.AddWithValue("@lastname", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["lastname"].ToString()));
                        comm.Parameters.AddWithValue("@car", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CARS"].ToString()));
                        comm.Parameters.AddWithValue("@rent", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["RENT"].ToString()));
                        comm.Parameters.AddWithValue("@client", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].ToString()));

                        comm.CommandText = StrQuery;

                        conn.Open();
                        comm.ExecuteNonQuery();
                        conn.Close();

                    }
                }

        }
        catch (Exception ex)
        {
            throw;
        }

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

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