farderce 发表于 2008-7-2 16:44

操作文本

1、将文本中的数据导入到SQL数据库中。
2、把文本当数据库的使用,调用它里面的内容,实现功能
哪位大虾有这方面的事例,拿来看看。我叫要事例,麻烦不要给我说做的过程方法,因为急用。
希望能理解,再次十分感谢。

cnhacks 发表于 2008-7-2 19:45

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)
            {
                System.IO.StreamReader reader = new System.IO.StreamReader
                    (
                    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
                {
                    cmd.CommandText = "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) + "条");

        }

页: [1]

编程论坛