Sql C#附近语法不正确

我试图制作一个代码,将我所有的数据从网格插入到表格中。在网格中我显示我需要的东西,这不是问题,或者它没有给出错误


显示此错误:


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;

                }

更新了参数。


string StrQuery;

            try

            {

                using (SqlConnection conn = new SqlConnection(stringcon))

                {

                    using (SqlCommand comm = new SqlCommand())

                    {

                        comm.Connection = conn;

                        conn.Open();


                        }

                    }

                }

            }


现在它给出了一个不同的错误:


System.FormatException:“输入字符串的格式不正确。”

图片: capture1 capture25 capture25 capture25 capture5


冉冉说
浏览 89回答 3
3回答

HUH函数

bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString())为您提供方法的重写实现ToString。这意味着您没有从上面的代码中获得实际值。你应该bunifuCustomDataGrid2.Rows[i].Cells["firstname"].Value改用。如果有帮助,请标记为已回答。

江户川乱折腾

我检查了您的代码并进行了此更改。您可以使用以下代码。&nbsp;string StrQuery;&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (SqlConnection conn = new SqlConnection(stringcon))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < bunifuCustomDataGrid2.Rows.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SqlCommand comm = new SqlCommand();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.Connection = conn;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StrQuery = @"INSERT INTO concediati(nume,prenume,idcar,idrent,idclient) VALUES (@name,@lastname,@car,@rent,@client)";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters.AddWithValue("@name", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters.AddWithValue("@lastname", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["lastname"].ToString()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters.AddWithValue("@car", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CARS"].ToString()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters.AddWithValue("@rent", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["RENT"].ToString()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters.AddWithValue("@client", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].ToString()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.CommandText = StrQuery;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn.Open();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.ExecuteNonQuery();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw;&nbsp; &nbsp; &nbsp; &nbsp; }

千万里不及你

INSERT的文档在表名和列列表之间显示了一个空格,因此最好遵循该空格。此外,您可以在循环外仅创建一次参数并在循环中设置它们的值(否则您需要对参数调用 .Clear() 并在每次迭代时重新创建它们):string sql = @"INSERT INTO concediati (nume, prenume, idcar, idrent, idclient) VALUES (@name, @lastname, @car, @rent, @client)";using (SqlConnection conn = new SqlConnection(stringcon)){&nbsp; &nbsp; using (SqlCommand comm = new SqlCommand(sql, conn))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters.Add(new SqlParameter { ParameterName = "@name", SqlDbType = SqlDbType.VarChar, Size = 50 });&nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters.Add(new SqlParameter { ParameterName = "@lastname", SqlDbType = SqlDbType.VarChar, Size = 50 });&nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters.Add(new SqlParameter { ParameterName = "@car", SqlDbType = SqlDbType.Int });&nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters.Add(new SqlParameter { ParameterName = "@rent", SqlDbType = SqlDbType.Int });&nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters.Add(new SqlParameter { ParameterName = "@client", SqlDbType = SqlDbType.Int });&nbsp; &nbsp; &nbsp; &nbsp; conn.Open();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < bunifuCustomDataGrid2.Rows.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string firstName = Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["firstname"].Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string lastName = Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["lastname"].Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int car = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CARS"].Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int rent = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["RENT"].Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int client = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters["@name"].Value = firstName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters["@lastname"].Value = lastName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters["@car"].Value = car;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters["@rent"].Value = rent;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.Parameters["@client"].Value = client;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comm.ExecuteNonQuery();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP