private void button3_Click_1(object sender, EventArgs e)
{
OpenFileDialog diag = new OpenFileDialog();
diag.Filter = "*.txt|*.txt";
diag.Title = "请选择你的txt文件";
if (diag.ShowDialog() == DialogResult.OK)
{
reader = new
(
diag.FileName,
Encoding.Default
//txt用什么编码,ascii,gb2312,unicode
);
textBox5.Text = diag.FileName;
string line = null;
listBox1.Items.Clear();
while ((line = reader.ReadLine()) != null)
{
listBox1.Items.Add(line);
//每一行数据,这里做你的处理
}
label6.Text = listBox1.Items.Count.ToString();
MessageBox.Show("总共有" + listBox1.Items.Count + "条记录!");
reader.Close();
}
}
private void button4_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
progressBar1.Maximum = listBox1.Items.Count;
foreach (string aa in listBox1.Items)
{
try
{
= "insert " + textBox4.Text + "(text_a,text_b) values('" + aa + "','" + bb + "')";
cmd.ExecuteNonQuery();
progressBar1.Value += 1;
listBox1.SelectedItem = listBox1.Items[listBox1.Items.Count - 1];
}
catch (Exception ex)
{
}
}
MessageBox.Show("总共有记录" + label6.Text + "条\n成功" + (Convert.ToInt32(label6.Text) - Convert.ToInt32(progressBar1.Maximum - progressBar1.Value)).ToString() + "条\n失败" + Convert.ToInt32(progressBar1.Maximum - progressBar1.Value) + "条");
}