功能:很简单的一个查询小程序 没什么技术 只是当做对于控件和一些类知识的理解
程序图示:
代码如下:
首先要用到一个“应用程序配置文件App.config”
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <connectionStrings> 4 <add name="connStr" connectionString="Data Source=ZIP-CW\MSSQLSERVER0;Initial Catalog=MySchool;Integrated Security=True"/> 5 </connectionStrings> 6 </configuration>
写好这个配置文件后,还要添加引用,在.NET中 选择System.Configuration;之后在Using System.Configuration就可以了。
下面正式开始功能实现部分,这个部分最主要的问题在于where关键字什么时候有,什么时候没有。
当三个条件都不选择的时候,就没有用到where;当三个里面至少有一个设置了查询条件,那么这时候就要用到where了。
有两种方法可以做,一种是简单一些的利用where 1=1 (这个方法有很多问题,这里不讨论),还有一种设置了一个bool类型的参数作为判断。下面我将把两个方法都写一下。ps:这里面还有很多的处理方法,不在继续讨论了
①where 1=1
1 using System.Data.SqlClient; 2 using System.Configuration; 3 4 namespace WfmDs 5 { 6 public partial class Form1 : Form 7 { 8 public Form1() 9 { 10 InitializeComponent(); 11 } 12 13 string conStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; //调用App.Config文件 14 15 private void btnSearch_Click(object sender, EventArgs e) 16 { 17 string sql = "select * from [Student] where 1=1"; //这里用到了where 1=1 18 if (txtName.Text != "") 19 { 20 sql = sql + string.Format(" and sName like '%{0}%'",txtName.Text.Trim()); //记得加单引号 21 } 22 if (txtSex.Text != "") 23 { 24 sql=sql+string.Format(" and sSex like '{0}'",txtSex.Text.Trim()); //Trim()方法去掉空格 25 } 26 if (txtAge.Text != "") 27 { 28 sql = sql + string.Format(" and sAge like {0}",txtAge.Text.Trim()); 29 } 30 GetStudent(sql); //调用方法 31 32 } 33 34 private void Form1_Load(object sender, EventArgs e) 35 { 36 string sql = "select * from [Student]"; 37 GetStudent(sql); //调用方法 38 } 39 40 private void GetStudent(string sql) 41 { 42 using (SqlConnection conn = new SqlConnection(conStr)) 43 { 44 DataSet ds = new DataSet(); //构造一个数据集 45 SqlDataAdapter sda = new SqlDataAdapter(sql, conn); //用法和SqlCommand一样 46 sda.Fill(ds); //查询数据库 47 dgvStudent.DataSource = ds.Tables[0]; //把这个数据集的第0个表作为DataGridView数据源 48 } 49 } 50 51 } 52 }
②设置参数判读where
1 namespace WfmDs 2 { 3 public partial class BetterSelect : Form 4 { 5 public BetterSelect() 6 { 7 InitializeComponent(); 8 } 9 10 string conStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; 11 12 private void btnSearch_Click(object sender, EventArgs e) 13 { 14 StringBuilder sql = new StringBuilder("select * from [Student]"); 15 bool hasWhere = false; 16 if (txtName.Text != "") 17 { 18 hasWhere = AppendWhere(sql,hasWhere); 19 sql.AppendLine(" sName like '%"+txtName.Text.Trim()+"%'"); 20 } 21 if(txtSex.Text!="") 22 { 23 hasWhere = AppendWhere(sql,hasWhere); 24 sql.AppendLine(" sSex like '"+txtSex.Text.Trim()+"'"); 25 } 26 if (txtAge.Text != "") 27 { 28 hasWhere = AppendWhere(sql, hasWhere); 29 sql.AppendLine(" sAge like "+txtAge.Text.Trim()); 30 } 31 GetStudent(sql.ToString()); 32 } 33 34 private bool AppendWhere(StringBuilder sql, bool hasWhere) //判断WHERE是否存在的方法 35 { 36 if (hasWhere == false) 37 { 38 sql.AppendLine(" where"); 39 return true; //这里返回true 40 } 41 else 42 { 43 sql.AppendLine(" and"); 44 return true; //这里也要返回true 45 } 46 } 47 48 private void BetterSelect_Load(object sender, EventArgs e) 49 { 50 string sql = "select * from [Student]"; 51 GetStudent(sql); 52 txtClassId.DataBindings.Add("Text", dgvStudent.DataSource, "sClassId"); //这里用到了一个DataBindings属性,功能是鼠标选中的DataGridView 数据源的sClassId字段放到TextBox控件的Text中。 53 } 54 55 private void GetStudent(string sql) 56 { 57 using (SqlConnection conn = new SqlConnection(conStr)) 58 { 59 DataSet ds = new DataSet(); 60 SqlDataAdapter sda = new SqlDataAdapter(sql, conn); 61 sda.Fill(ds); 62 dgvStudent.DataSource=ds.Tables[0]; 63 } 64 } 65 } 66 }
这两个方法就是上面讲的这些。
补充一个控件:
txtClassId.DataBindings.Add("Text", dgvStudent.DataSource, "sClassId");这里用到了一个DataBindings属性,功能是鼠标选中的DataGridView 数据源的sClassId字段放到TextBox控件的Text中。
ADO.NET结构图:(方便理解)