【问题标题】:Repeat read specific number of line into a text file, show them into a textboxes and save them重复将特定行数读入文本文件,将它们显示到文本框中并保存
【发布时间】:2015-07-14 18:23:44
【问题描述】:

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

【问题讨论】:

  • 将整个文件读入列表并从那里一次处理 8 行?

标签: c# textbox text-files


【解决方案1】:

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

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-statement,以确保所有非托管资源都被释放属性(即使出现错误)。

也总是使用 sql 参数来防止 sql 注入。

【讨论】:

  • 我尝试过这种方式:pastebin.com/nAy8h47a 但我在这里遇到错误:textBox6.Text = line[5].ToString(); 中的“矩阵索引超出限制”;请问我该怎么办? :)
  • @marcellogabrielli:你想做什么? line 是文件的单行,因此您不能使用 line[0].ToString(),因为它只会占用该行的第一个字符。
  • 我需要同时在 8 个文本框中插入 8 行,并继续插入到文本文件的末尾,例如0-7行、8-15行、16-23行等
  • @marcellogabrielli:你有多少个文本框?是否要将第 0-7 行插入 textbox1-8 并将第 8-15 行插入 textbox9-16 并将第 16-23 行插入 textboxes17-24 等等?
  • 不,我想这样做:将0-7行放入textbox1-8并将它们保存到db中;将文本框 1-8 第 8-15 行清除到 textbox1-8 中并将它们保存到 db 中;将文本框 1-8 第 16-23 行清除到 textbox1-8 并将它们保存到 db 中;清除文本框 1-8 我知道如何保存它们,但我不知道如何将其他行放在同一个文本框中,直到文本文件结束:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多