我想知道i ++和++ i在循环中有什么区别 [英] I want to know what is the difference between i++ and ++i in loop

查看:93
本文介绍了我想知道i ++和++ i在循环中有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,我想知道i +和++ i之间的区别在循环bcz中我在我的项目中使用i ++并且它在数据库中增加了额外的价值所以我需要知道它之间的区别。



我尝试了什么:



Hello Guys, I want to know the difference between i++ and ++i in loop bcz i am using i++ in my project and it's add extra value in database so i need to know the difference between it.

What I have tried:

for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {
                        connection.Open();
                        OleDbCommand command = new OleDbCommand();
                        command.Connection = connection;
                        command.CommandText = "insert into Total ([Column1],[Column2],[Column3],[Date],[Receipt No],[Delivery Person],[Report],[Flavours],[Return])values('" + dataGridView1.Rows[j].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[j].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[j].Cells[2].Value.ToString() + "','" + dataGridView1.Rows[j].Cells[4].Value.ToString() + "','" + label2.Text + "','" + "---" + "'," + dataGridView1.Rows[j].Cells[0].Value.ToString() + ",'" + dataGridView1.Rows[j].Cells[3].Value.ToString() + "','" + textBox66.Text + "');";
                        command.ExecuteNonQuery();
                        connection.Close();
                    }

                    MessageBox.Show("Inserted Sucessfully", "Database", MessageBoxButtons.OK, MessageBoxIcon.Information);

推荐答案

你可能有 AllowUserToAddRows [ ^ ]属性设置为 true ,并且您正在插入值空的插入行。检查 IsNewRow [< a href =https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.isnewrow(v=vs.110).aspx\"target =_ blanktitle =New Window尝试插入值之前的^ ^ ]属性。



您还需要修复 SQL注入 [ ^ ]您的代码中的漏洞。



最好打开一次连接,而不是每行打开和关闭它。

You probably have the AllowUserToAddRows[^] property set to true, and you're inserting the values from the empty "insert" row. Check the IsNewRow[^] property before trying to insert the values.

You'll also want to fix the SQL Injection[^] vulnerability in your code.

And it would be best to open the connection once, rather than opening and closing it for every row.
const string Query = @"INSERT INTO Total ([Column1], [Column2], [Column3], [Date], [Receipt No], [Delivery Person], [Report], [Flavours], [Return])
VALUES (@Column1, @Column2, @Column3, @Date, @ReceiptNo, @DeliveryPerson, @Report, @Flavours, @Return)";

using (var connection = new OleDbConnection("..."))
using (var command = new OleDbCommand(connection, Query))
{
    connection.Open();
    
    for (int j = 0; j < dataGridView1.Rows.Count; j++)
    {
        var row = dataGridView1.Rows[j];
        if (row.IsNewRow) continue;
        
        command.Parameters.Clear();
        command.Parameters.AddWithValue("@Column1", row.Cells[0].Value);
        command.Parameters.AddWithValue("@Column2", row.Cells[1].Value)
        command.Parameters.AddWithValue("@Column3", row.Cells[2].Value);
        command.Parameters.AddWithValue("@Date", row.Cells[4].Value);
        command.Parameters.AddWithValue("@ReceiptNo", label2.Text);
        command.Parameters.AddWithValue("@DeliveryPerson", "---");
        command.Parameters.AddWithValue("@Report", row.Cells[0].Value);
        command.Parameters.AddWithValue("@Flavours", row.Cells[3].Value);
        command.Parameters.AddWithValue("@Return", textBox66.Text);
        
        command.ExecuteNonQuery();
    }
}





一旦你解决了这个问题,请帮自己一个忙,给你的控件一个有意义的名字而不是接受Visual Studio提供的默认名称。您可能还记得 textBox66 意味着现在,但是当您在几个月内回到代码中时,您将不会有任何线索。



Once you've fixed that, do yourself a favour and give your controls meaningful names, instead of accepting the default name that Visual Studio provides. You might remember what textBox66 means now, but when you come back to your code in a few months, you won't have a clue.


不同之处在于操作顺序:



  • i ++,返回i的值然后返回增量
  • ++ i,先增加,然后返回i的值
The difference is in the order of operations:

  • i++, returns the value of i and then increments
  • ++i, increments first and then returns the value of i


这篇关于我想知道i ++和++ i在循环中有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆