C# 两个库对比 请高手指点
最近在学C# 自己写了个sql两个库对比的程序 用线程的话 不知道怎么写才好
代码如下:
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.Data.SqlClient;
namespace songs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int flag = 0,n=0;
public Thread t,t1;
public delegate void myd();
public void RunINinvoke()
{
if (dataGridView1.InvokeRequired)
{
dataGridView1.Invoke(new myd(db_data));
}
else
{
db_data();
}
}
public class getdata
{
public SqlConnection CC_Con()
{
string connstr = "Data Source=.;Initial Catalog=xiaoqiang;Persist Security Info=True;User ID=sa;Password=;Asynchronous Processing=true;Max Pool Size=512";
return new SqlConnection(connstr);
}
}
public void db_data()
{
if (textBox1.Text == "" && textBox2.Text == "")
{
return;
}
getdata gd = new getdata();
string str = "select * from dbo." + textBox2.Text + "";
SqlConnection conn = gd.CC_Con();
conn.Open();
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
BindingSource bs = new BindingSource();
bs.DataSource = sdr;
dataGridView2.DataSource = bs;
sdr.Close();
string str1 = "select * from dbo." + textBox1.Text + "";
cmd = new SqlCommand(str1, conn);
sdr = cmd.ExecuteReader();
while (sdr.Read())
{
string s1 = sdr[0].ToString().Trim();
s1 = s1.Replace(" ", "");
s1 = s1.Replace(";", "");
string s2 = sdr[1].ToString().Trim();
s2 = s2.Replace(" ", "");
s2 = s2.Replace(";", "");
string s3 = sdr[2].ToString().Trim();
s3 = s3.Replace(" ", "");
s3 = s3.Replace(";", "");
for (int j=0; j < dataGridView2.Rows.Count;j++ )
{
string s11 = dataGridView2.Rows[j].Cells[0].Value.ToString().Trim();
s11 = s11.Replace(" ", "");
s11 = s11.Replace(";", "");
string s21 = dataGridView2.Rows[j].Cells[1].Value.ToString().Trim();
s21 = s21.Replace(" ", "");
s21 = s21.Replace(";", "");
string s31 = dataGridView2.Rows[j].Cells[2].Value.ToString().Trim();
s31 = s31.Replace(" ", "");
s31 = s31.Replace(";", "");
if (s1 == s11)
{
if (s2 == s21)
{
if (s3 == s31)
{
flag = 1;
break;
}
}
}
}
if (flag==0)
{
dataGridView1.Rows.Add(s1, s2, s3);
}
else
{
flag = 0;
}
}
if (dataGridView1.Rows.Count > 0)
{
dataGridView1.Rows.RemoveAt(0);
}
this.Text = "歌曲对比";
toolStripStatusLabel1.Text = "共有: " + (dataGridView1.Rows.Count - 1).ToString() + " 首歌曲";
toolStripStatusLabel2.Text = " 用时:" + n+"秒";
button1.Enabled = true;
timer1.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
textBox1.Focus();
return;
}
if (textBox2.Text == "")
{
textBox2.Focus();
return;
}
timer1.Enabled = true;
n = 0;
this.Text = "正在处理...";
toolStripStatusLabel1.Text = "";
toolStripStatusLabel2.Text = "";
button1.Enabled = false;
dataGridView1.Rows.Clear();
CheckForIllegalCrossThreadCalls = false;
t = new Thread(new ThreadStart(db_data));
//t = new Thread(new ThreadStart(RunINinvoke));
t.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
n++;
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Columns.Add("歌曲名称", "歌曲名称");
dataGridView1.Columns.Add("歌星姓名", "歌星姓名");
dataGridView1.Columns.Add("语种", "语种");
}
private void texBox2_KeyUp(object sender, EventArgs e)
{
button1_Click(sender, e);
}
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
}
}
如果用 t = new Thread(new ThreadStart(RunINinvoke));
这个的话 UI 就会卡死
请高手指点