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;
}
}
}
}
}
}