尝试使用参数迭代地将datagridview的值插入表中

时间:2017-09-13 11:57:30

标签: c# sql datagridview parameters

所以我一直在尝试使用datagridview将新行添加到我的数据库中。我不希望事先将整个数据库加载到网格中并刷新它。我只想将datagrid中的值添加到表中。我无法找到解决方法。如果我这样做,它会给我一个错误,即所有变量都必须是唯一的。我试图添加sqlconnection.parameters.clear()函数,但似乎它使所有值变为空并给我一个错误,我没有提供参数的必要信息。

string TicketTableInsertString = "INSERT INTO ticketTable (TicketID, num1, num2,num3,num4,num5) VALUES (@TicketID,@num1,@num2,@num3,@num4,@num5)";

        SqlConnection sqlConn;
        connString = "Data Source=DESKTOP-30191JE\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        sqlConn = new SqlConnection(connString);

        try
        {
            sqlConn.Open();
            int ticketID=0;
            SqlCommand selectTicketIDCMD = new SqlCommand(selectTicketIDString, sqlConn);
            SqlCommand mainTableInsertCMD = new SqlCommand(MainTableInsertString, sqlConn);
            SqlCommand ticketTableInsertCMD = new SqlCommand(TicketTableInsertString, sqlConn);
            ticketID = (int)selectTicketIDCMD.ExecuteScalar()+1;
            sqlConn.Close();


            for (int i = 0; i < dgv_ticket.Rows.Count; i++)
            {

                ticketTableInsertCMD.Parameters.AddWithValue("@TicketID", ticketID);             
                ticketTableInsertCMD.Parameters.AddWithValue("@num1", dgv_ticket.Rows[i].Cells[0].Value);
                ticketTableInsertCMD.Parameters.AddWithValue("@num2", dgv_ticket.Rows[i].Cells[1].Value);
                ticketTableInsertCMD.Parameters.AddWithValue("@num3", dgv_ticket.Rows[i].Cells[2].Value);
                ticketTableInsertCMD.Parameters.AddWithValue("@num4", dgv_ticket.Rows[i].Cells[3].Value);
                ticketTableInsertCMD.Parameters.AddWithValue("@num5", dgv_ticket.Rows[i].Cells[4].Value);
                sqlConn.Open();
                ticketTableInsertCMD.ExecuteNonQuery();
                sqlConn.Close();

            }

2 个答案:

答案 0 :(得分:0)

在for循环开始时清除sqlCommand参数并为每行设置ticketID:

for (int i = 0; i < dgv_ticket.Rows.Count; i++)
        {
            ticketID = (int)selectTicketIDCMD.ExecuteScalar()+1;
            ticketTableInsertCMD.Parameters.Clear();
            ticketTableInsertCMD.Parameters.AddWithValue("@TicketID",  ...

答案 1 :(得分:0)

找到了正确的方法我在循环外添加了参数,然后在循环中更改了它们的值。

                ticketTableInsertCMD.Parameters.Add("@TicketID", SqlDbType.Int);
                ticketTableInsertCMD.Parameters.Add("@num1", SqlDbType.Int);
                ticketTableInsertCMD.Parameters.Add("@num2", SqlDbType.Int);
                ticketTableInsertCMD.Parameters.Add("@num3", SqlDbType.Int);
                ticketTableInsertCMD.Parameters.Add("@num4", SqlDbType.Int);
                ticketTableInsertCMD.Parameters.Add("@num5", SqlDbType.Int);



            for (int i = 0; i < dgv_ticket.Rows.Count-1; i++)
            {
                ticketTableInsertCMD.Parameters["@TicketID"].Value = ticketID;
                ticketTableInsertCMD.Parameters["@num1"].Value = int.Parse(dgv_ticket.Rows[i].Cells[0].Value.ToString());
                ticketTableInsertCMD.Parameters["@num2"].Value = int.Parse(dgv_ticket.Rows[i].Cells[1].Value.ToString());
                ticketTableInsertCMD.Parameters["@num3"].Value = int.Parse(dgv_ticket.Rows[i].Cells[2].Value.ToString());
                ticketTableInsertCMD.Parameters["@num4"].Value = int.Parse(dgv_ticket.Rows[i].Cells[3].Value.ToString());
                ticketTableInsertCMD.Parameters["@num5"].Value = int.Parse(dgv_ticket.Rows[i].Cells[4].Value.ToString());