C#线程的基本应用(数据导出,与数据转移)

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.Data.SqlClient;
using System.IO;
using System.Net;
using System.Threading;

namespace 线程操作Winfor
{
    public partial class DataBaseToTxt : Form
    {
        public DataBaseToTxt()
        {
            InitializeComponent();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
           
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            using (SqlConnection con = new SqlConnection("server=.\\sqlexpress;initial catalog=;integrated security=true"))
            {
                using (SqlCommand cmd = new SqlCommand("select * from T_City",con))
                {
                        DataTable dt = new DataTable();
                        using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                        {
                         
                            sda.Fill(dt);
                        }
                        string[] msg = new string[1];
                        int i=1;
                        foreach (DataRow item in dt.Rows)
                        {
                                msg[0] = item[0].ToString() + "|" + item[1].ToString() + "|" + item[2].ToString();
                                File.AppendAllLines(Application.StartupPath+"DataBase.txt",msg);
                                int numb = i * 100 / dt.Rows.Count;
                                this.backgroundWorker1.ReportProgress(numb);
                                i++;
                        }
                        MessageBox.Show("导出成功!!!"); 
                     
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state)
            {
                //FristMethod(state);   //第一种方法导出


                SecondMethod(state);
            }));
          
        }

        private void SecondMethod(object state)
        {
           // throw new NotImplementedException();
            DataTable table1 = SQLDataTable("select * from hat_area",CommandType.Text);
            int talbe1nums = table1.Rows.Count;

            foreach (DataRow item in table1.Rows)
            {
                SQLInsert("insert hat_area1 values(@name1,@name2,@name3)",CommandType.Text,
                    new SqlParameter("@name1",item[1].ToString()),
                    new SqlParameter("@name2",item[2].ToString()),
                    new SqlParameter("@name3",item[3].ToString())
                    );
                DataTable table2 = SQLDataTable("select * from hat_area1",CommandType.Text);
               int talbe2nums = table2.Rows.Count;
                if (talbe1nums<=talbe2nums)
                {
                    MessageBox.Show("导入完成!!!!");
                }
            }
        }

       

        private void FristMethod(object state)
        {
            int numb = Convert.ToInt32(state);
            SQLInsert("insert into hat_area1(areaID, area, father) select areaID, area, father from hat_area", CommandType.Text);

            DataTable dt = SQLDataTable("select count(*) from hat_area", CommandType.Text);
            int table1nums = dt.Rows.Count;   //表1的总行数据

            DataTable dt1 = SQLDataTable("select count(*) from hat_area1", CommandType.Text);
            int table2nums = dt.Rows.Count;   //表1的总行数据
            while (true)
            {
                if (table1nums == table2nums)
                {
                    MessageBox.Show("导出完成!!");
                    break;
                }
            }
        }

        private void SQLInsert(string sqlcmd, CommandType commandtype, params SqlParameter[] param)
        {

            using (SqlConnection con = new SqlConnection("server=.\\sqlexpress;initial catalog=Mytest1;integrated security=true"))
            {
                using (SqlCommand cmd = new SqlCommand(sqlcmd,con))
                {
                    con.Open();
                    cmd.Parameters.AddRange(param);
                    cmd.CommandType = commandtype;
                    cmd.ExecuteNonQuery();
                }
            }


        }

        private DataTable SQLDataTable(string sqlcmd, CommandType commandtype, params SqlParameter[] param)
        {

            using (SqlConnection con = new SqlConnection("server=.\\sqlexpress;initial catalog=Mytest1;integrated security=true"))
            {
                using (SqlCommand cmd = new SqlCommand(sqlcmd, con))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }


        }

 

 

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值