c#数据查询例子-线程

  • 格式:docx
  • 大小:287.18 KB
  • 文档页数:7

C#数据查询例子-线程(张宗杰)因为教学需要给学生讲解线程的知识,所以做了个数据查询的例子(登录),在这里采用原始的数据查询方法和采用线程的数据查询方法,让大家做个比较。

首先做出界面。

图1 使用线程图2 没有使用线程在这里,我就不介绍数据库的内容了。

先来看没有使用线程的代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;//using System.Data.SqlClient;namespace myxc{publicpartialclass Form2 : Form{privatebool LoginOK(string uid, string pwd){bool result = false;try{string con = "server=AJIE-PC\\SQLEXPRESS;uid=sa;pwd=201215;database=QQ";string sql = "select uid,upwd from QQ_admin where uid='" + uid + "' and upwd='" + pwd + "'"; SqlConnection mycon = new SqlConnection(con);mycon.Open();SqlCommand cmd = new SqlCommand(sql, mycon);SqlDataReader myread = cmd.ExecuteReader();if (myread.Read()){result = true;}}catch{result = false;}return result;}public Form2(){InitializeComponent();}privatevoid button1_Click(object sender, EventArgs e){if (this.textBox1.Text.Trim().Length > 0){if (this.textBox2.Text.Trim().Length > 0){this.textBox1.Enabled = false;this.textBox2.Enabled = false;this.button1.Enabled = false;this.button2.Enabled = false;bel3.Text = "正在登录……";this.pictureBox1.Visible = true;bool result = this.LoginOK(this.textBox1.Text.Trim(), this.textBox2.Text.Trim()); if (result == true){this.textBox1.Enabled = true;this.textBox2.Enabled = true;this.button1.Enabled = true;this.button2.Enabled = true;bel3.Text = "登录成功!!!";this.pictureBox1.Visible = false;}elseif (result == false){this.textBox1.Enabled = true;this.textBox2.Enabled = true;this.button1.Enabled = true;this.button2.Enabled = true;bel3.Text = "登录失败!!!";this.pictureBox1.Visible = false;}}else{MessageBox.Show("请输入密码", "信息提示?", MessageBoxButtons.OK, MessageBoxIcon.Warning);this.textBox2.Focus();}}else{MessageBox.Show("请输入帐号!", "信息提示?", MessageBoxButtons.OK, MessageBoxIcon.Warning);this.textBox1.Focus();}}privatevoid Form2_Load(object sender, EventArgs e){this.pictureBox1.Visible = false;}}}该程序执行后的效果图如下:如果细心的同学会发现,我们已经在窗体中放入了一个图片,在你点击登录后,该图片会出现,并且不断的转动,并且会显示“正在登录……”,但是使用该方法没有这种效果,检查代码,我们的代码没有问题。

要实现我们需要的效果必须采用线程。

顺便说一下,使用这种方法,你点击了登录后,你用鼠标是拖动不了窗体的。

我们来看一下,采用线程后的效果图:效果图看到了,这时来看看代码(注意和没有使用线程的做区别)。

using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;//using System.Data.SqlClient;using System.Threading;namespace myxc{publicpartialclass Form1 : Form{privatevoid ChangeMessage(string txt){this.Invoke((MethodInvoker)delegate{bel3.Text = txt;});}privatebool LoginOK(string uid,string pwd){bool result = false;try{string con = "server=AJIE-PC\\SQLEXPRESS;uid=sa;pwd=201215;database=QQ";string sql = "select uid,upwd from QQ_admin where uid='" + uid + "' and upwd='" + pwd + "'"; SqlConnection mycon = new SqlConnection(con);mycon.Open();SqlCommand cmd = new SqlCommand(sql, mycon);SqlDataReader myread = cmd.ExecuteReader();if (myread.Read()){result = true;}}catch{result = false;}return result;}privatevoid myLogin(){bool isLogin = false;this.Invoke((MethodInvoker)delegate{this.textBox1.Enabled = false;this.textBox2.Enabled = false;this.button1.Enabled = false;this.button2.Enabled = false;this.pictureBox1.Visible = true;});this.ChangeMessage("正在登录......");isLogin = this.LoginOK(this.textBox1.Text.Trim(), this.textBox2.Text.Trim()); if (isLogin == true){this.ChangeMessage("登录成功!!!!");this.Invoke((MethodInvoker)delegate{this.textBox1.Enabled = true;this.textBox2.Enabled = true;this.button1.Enabled = true;this.button2.Enabled = true;this.pictureBox1.Visible = false;});}elseif (isLogin == false){this.ChangeMessage("登录失败!!!!");this.Invoke((MethodInvoker)delegate{this.textBox1.Enabled = true;this.textBox2.Enabled = true;this.button1.Enabled = true;this.button2.Enabled = true;this.pictureBox1.Visible = false;});}}public Form1(){InitializeComponent();}privatevoid button1_Click(object sender, EventArgs e){if (this.textBox1.Text.Trim().Length > 0){if (this.textBox2.Text.Trim().Length > 0){Thread myth = new Thread(myLogin);myth.IsBackground = true;myth.Start();}else{MessageBox.Show("请输入登录密码", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); this.textBox2.Focus();}}else{MessageBox.Show("请输入登录帐号!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); this.textBox1.Focus();}}privatevoid Form1_Load(object sender, EventArgs e){this.pictureBox1.Visible = false;}}}。