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



我读取了一个文本文件(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

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

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=0groupOf8=0中直到linenumber=7,然后是groupOf8=1,以此类推。

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

始终使用sql参数来防止sql注入

相关内容

最新更新