日期:2014-05-18  浏览次数:21179 次

用了线程发生"未将对象的引用设置到对象的实例"
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Data.SqlClient;
namespace 多线程综合
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //Control.CheckForIllegalCrossThreadCalls = false;
        }
        int  index;
        string tb1name;
        string str;
        DataTable dt;
        SqlBulkCopy sbc;
        SqlTransaction sqltran;
        SqlConnectionStringBuilder sqlconnsb = new SqlConnectionStringBuilder();
        SqlConnection conn;
        public ManualResetEvent mre = new ManualResetEvent(false);
        //public ManualResetEvent mre2 = new ManualResetEvent(false);
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择文件";
            ofd.CheckFileExists = true;
            ofd.Filter = "All Files|*.*";
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
                index = this.textBox1.Text.LastIndexOf("\\");
                tb1name = textBox1.Text;
            }
            else
            {
                return;
            }
        }
        public void traversal()
        {
            Console.WriteLine("process in traversal");
            SqlDataAdapter sda = new SqlDataAdapter();
            int loop = 0;
            string filename;
            string packagequeue;
            string jlfsrq;
            DataSet ds = new DataSet("details");
            DataRow dr;
            string srtxt;
            string[] Myfolder = Directory.GetFiles(tb1name.Substring(0, index), "FH*A");
            using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=InsertTest;Integrated Security=True"))
            {
            for (int i = loop; i < Myfolder.Length; i++)
            {
                FileInfo fi = new FileInfo(Myfolder[i]);
                if (fi.FullName == tb1name)
                {
                    loop = i + 1;
                    str = "select * from details";
                    sda = new SqlDataAdapter(str, conn);
                    sda.FillSchema(ds, SchemaType.Mapped, "details");
                    dt = ds.Tables[0].Clone();//复制表结构
                    ds.Dispose();

                    filename = this.textBox1.Text.Substring(index + 1, tb1name.Length - index - 1);
                    jlfsrq = "20" + filename.Substring(2, 6).Trim();
                    packagequeue = filename.Substring(21, 3).Trim();
                    using (StreamReader sr = new StreamReader(textBox1.Text))
                    {
                        int a = 0;
                        while (!sr.EndOfStream)
                        {
                            srtxt = sr.ReadLine().Trim();
                            if (a > 0)
                            {
                                dr = dt.NewRow();
                                dr["hh"] = Convert.ToInt32(srtxt.Substring(11, 8));
                                //Console.WriteLine(Convert.ToInt32(srtxt.Substring(11, 8)));
                                dr["stationid"] = srtxt.Substring(21, 6);
                                //Console.WriteLine(srtxt.Substring(21, 6));
                                dr["sskh"] = srtxt.Substring(35, 8);
                                dt.Rows.Add(dr);
                            }
                            a++;
                        }