在按钮上单击INSERT表中的许多控件的多个(文本框和标签)文本

时间:2015-08-05 06:28:21

标签: c# mysql sql .net sql-server-ce

我创建了表“TenOperations”,其中有4列“EUR”类型为真,“Rate”类型为real,“BGN”类型为real,“Date”类型为nvarchar

在按钮上单击我试图插入所有填充的文本框和标签中的数据,以便:

第一行应该是

 textbox1.text , Rate , textbox2.text , Date2.text

第二行应该是

 textbox3.text , Rate , textbox4.text , Date4.text

等等

Rate是全局双变量,Date2是Label,当textbox2.text被更改时,它会更改文本。

问题是我的代码创建了具有相等值的无限行

private void InsertData_Click(object sender, EventArgs e)
{
            var textboxes = new List<TextBox>() {
                textBox1,
                textBox2,
                textBox3,
                textBox4,
                textBox5,
                textBox6,
                textBox7,
                textBox8,
                textBox9,
                textBox10,
                textBox11,
                textBox12,
                textBox13,
                textBox14,
                textBox15,
                textBox16,
                textBox17,
                textBox18,
                textBox19,
                textBox20
            };
            var labels = new List<Label>() {
                Date2,
                Date4,
                Date6,
                Date8,
                Date10,
                Date12,
                Date14,
                Date16,
                Date18,
                Date20        
            };

            SqlCeConnection connection = new SqlCeConnection(@"Data Source=C:\Users\FluksikartoN\Documents\Visual Studio 2012\Projects\BuroFoki\BuroFoki\MainDB.sdf");
            connection.Open();

            for (int i = 0; i < 10 ; i = i++)
            { 

                using (SqlCeCommand com = new SqlCeCommand("INSERT INTO TenOperations (EUR, Rate, BGN, Date) Values(@EUR, @Rate, @BGN, @Date)", connection))
                {


                    com.Parameters.AddWithValue("@EUR", textboxes[i+1].Text.ToString());
                    com.Parameters.AddWithValue("@Rate", EURbuy);
                    com.Parameters.AddWithValue("@BGN", textboxes[i].Text.ToString());
                    com.Parameters.AddWithValue("@Date", labels[i].Text.ToString());

                    com.ExecuteNonQuery();
                }
            }
            connection.Close();
        }

1 个答案:

答案 0 :(得分:2)

for (int i = 0; i < 10 ; i = i++)

应该是

for (int i = 0; i < 10 ;i++)