【发布时间】: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