重复读取特定行数到文本文件中,将它们显示到文本框中并保存

时间:2015-07-14 08:07:54

标签: c# textbox text-files

我读了一个文本文件(8行),将它们显示在文本框中并将它们保存到数据库中。我已经做到了。但我需要继续阅读文本文件(每次8行)。

这是我的代码:

 var textBoxes = new List<TextBox> { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8 };
        using (StreamReader sr = new StreamReader(@"saverisbex.txt"))
        {
            int incNumber = 0;
            string nyNumber = incNumber.ToString("00");
            incNumber++;

            textBox9.Text = incNumber.ToString();

            int lineNumber = 0;
            int lastGroup = 0;
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                int currentGroup = lineNumber / 8;
                if (lastGroup != currentGroup)
                {
                    conn.Open();



                    SqlCommand comando = new SqlCommand("", conn);

                    comando.CommandText = "insert into finabex (Id,home,away,homescoresft,awayscoresft,oddhome,oddx,oddaway,date) values ('" +
                        textBox9.Text + "', '" +
                        textBox1.Text + "', '" +
                        textBox2.Text + "', '" +
                        textBox3.Text + "', '" +
                        textBox4.Text + "', '" +
                        textBox5.Text + "', '" +
                        textBox6.Text + "', '" +
                        textBox7.Text + "', '" +
                        textBox8.Text + "')";


                    comando.ExecuteNonQuery();
                    MessageBox.Show("Saved!");
                    conn.Close();
                }
                int textBoxNumber = lineNumber % 8;
                textBoxes[textBoxNumber].Text = line;


                lastGroup = currentGroup;
                lineNumber++;
            }
        }

所以,我读了我的前8行(0-7),现在我需要继续(8-15),(16-23)等等。

我希望你能帮助我。谢谢大家!!! :d

1 个答案:

答案 0 :(得分:0)

您可以使用整数除法和% - 运算符。将所有文本框放入集合中:

var textBoxes = new List<TextBox> { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8 };

using(var conn = new SqlConnection("Connectionstring"))
using (StreamReader sr = new StreamReader(@"saverisbex"))
{
    int lineNumber = 0;
    int lastGroup = 0;
    string line;
    conn.Open(); // before the loop not in loop
    while((line = sr.ReadLine()) != null)
    {
        int currentGroup = lineNumber / 8;
        if (lastGroup != currentGroup)
        {
            // presuming that ID is the primary key and an identity column which auto generates its value on insert
            string insertSQL = @"insert into finabex (home,away,homescoresft,awayscoresft, oddhome,oddx,oddaway,date) 
                                 values (@home,@away,@homescoresft,@awayscoresft,@oddhome,@oddx,@oddaway,@date)";
            using (var comando = new SqlCommand(insertSQL, conn))
            {
                comando.Parameters.Add("@home", SqlDbType.VarChar).Value = textBox9.Text;
                comando.Parameters.Add("@away", SqlDbType.VarChar).Value = textBox1.Text;
                comando.Parameters.Add("@homescoresft", SqlDbType.VarChar).Value = textBox2.Text;
                comando.Parameters.Add("@awayscoresft", SqlDbType.VarChar).Value = textBox3.Text;
                comando.Parameters.Add("@oddhome", SqlDbType.VarChar).Value = textBox4.Text;
                comando.Parameters.Add("@oddx", SqlDbType.VarChar).Value = textBox5.Text;
                comando.Parameters.Add("@oddaway", SqlDbType.VarChar).Value = textBox7.Text;
                DateTime date;
                if (!DateTime.TryParse(textBox8.Text, out date))
                {
                    MessageBox.Show("Not a valid date: " + textBox8.Text);
                    continue;
                }
                comando.Parameters.Add("@date", SqlDbType.DateTime).Value = date;
                int insertedCount = comando.ExecuteNonQuery();
            }

            // ...
        }
        int textBoxNumber = lineNumber % 8;
        textBoxes[textBoxNumber].Text = line;
        // ...
        lastGroup = currentGroup;
        lineNumber++;
    }
}

所以linenumber=0位于groupOf8=0之后linenumber=7,然后是groupOf8=1,依此类推。

请注意,我在实现using的所有对象上使用了IDisposable - 语句,以确保所有非托管资源都处置属性(即使出现错误)。

也使用sql参数来防止sql-injection。