为了账号安全,请及时绑定邮箱和手机立即绑定

Sql C#附近语法不正确

Sql C#附近语法不正确

C#
冉冉说 2022-11-21 16:52:22
我试图制作一个代码,将我所有的数据从网格插入到表格中。在网格中我显示我需要的东西,这不是问题,或者它没有给出错误显示此错误: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
查看完整描述

3 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞

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

如果有帮助,请标记为已回答。


查看完整回答
反对 回复 2022-11-21
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

我检查了您的代码并进行了此更改。您可以使用以下代码。


 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;

        }


查看完整回答
反对 回复 2022-11-21
?
千万里不及你

TA贡献1784条经验 获得超9个赞

INSERT的文档在表名和列列表之间显示了一个空格,因此最好遵循该空格。


此外,您可以在循环外仅创建一次参数并在循环中设置它们的值(否则您需要对参数调用 .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();


        }

    }

}


查看完整回答
反对 回复 2022-11-21
  • 3 回答
  • 0 关注
  • 87 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信