这是我昨天晚上作的,自己觉得还是不够精简,查询速度不快。
我想用DATAVIEW应该能更快一些吧。
private void boolchk()
{
string sqlstr="select * from comp where";
if(this.checkBox1.Checked==true)
{
sqlstr+=" fzr='"+this.textBox1.Text.Trim()+"'"+" and ";
}
if(this.checkBox2.Checked==true)
{
sqlstr+=" devicename='"+this.textBox2.Text.Trim()+"' and ";
}
if(this.checkBox3.Checked==true)
{
sqlstr+=" netcode='"+this.textBox3.Text.Trim()+"' and ";
}
if(this.checkBox4.Checked==true)
{
sqlstr+=" ipadd='"+this.textBox4.Text.Trim()+"' and ";
}
if(sqlstr.Length>24)//存在查询条件
{
sqlstr=sqlstr.Substring(0,sqlstr.Length-5);
this.ds.Clear();
this.ds=com.dst(sqlstr,tablename);//com.dst()为数据操作类Com的函数,它返回一个数据集
this.dataGrid1.DataSource=this.ds.Tables[tablename];
}
}
private void button1_Click(object sender, System.EventArgs e)
{
this.boolchk();
}
这是用DATAVIEW实现的,速度相当快,因为查询时不用再连接SQLSERVER。
界面也变了,回帖不会贴图,不好意思。
总体思路是用DATAVIEW的ROWFILTER属性实现查询。
代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Computer
{
/// <summary>
/// DeviceList 的摘要说明。
/// </summary>
public class DeviceList : System.Windows.Forms.Form
{
private ComC com=new ComC();
private DataTable table;
private DataView dv;
private string tablename="comp";
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public DeviceList()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.selectdb(); //把DATAGRID棒定到DATAVIEW
this.datagridbind(); //生成DATAGRID样式
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void datagridbind() //生成DATAGRID样式
{
DataGridTableStyle ts=new DataGridTableStyle();
ts.MappingName=this.table.TableName;
ts.AllowSorting=false;
DataGridTextBoxColumn td;
int numcols=this.table.Columns.Count;
for(int i=0;i<numcols;i++)
{
td=new DataGridTextBoxColumn();
td.MappingName=this.table.Columns[i].ColumnName;
td.HeaderText=this.table.Columns[i].ColumnName;
td.NullText="";
ts.GridColumnStyles.Add(td);
}
this.dataGrid1.TableStyles.Add(ts);
}
private void selectdb() //把DATAGRID棒定到DATAVIEW
{
string sqlstr="select devicename as 设备名称,spectype as 设备类型,entertime as 进厂时间,"+
"bm as 使用部门,fzr as 负责人,text as 备注,netcode as 网卡卡号,ipadd as IP地址 from comp order by 使用部门";
this.table=com.dt(sqlstr,this.tablename); //com.dt是数据操作类ComC的成员函数,它返回一
//个DataTable对象。
this.dv=new DataView(this.table);//创建DataView对象dv
this.dataGrid1.DataSource=dv;//把DataGrid绑定到dv上
}
private void button1_Click(object sender, System.EventArgs e)
{
string selstr="";//先把查询条件设置为空字符串
if(this.textBox1.Text.Trim()!="")
selstr+="设备名称='"+this.textBox1.Text.Trim()+"' and ";
if(this.textBox2.Text.Trim()!="")
selstr+="使用部门='"+this.textBox2.Text.Trim()+"' and ";
if(this.textBox3.Text.Trim()!="")
selstr+="负责人='"+this.textBox3.Text.Trim()+"' and ";
if(this.textBox4.Text.Trim()!="")
selstr+="进厂时间='"+this.textBox4.Text.Trim()+"' and ";
if(this.textBox5.Text.Trim()!="")
selstr+="网卡卡号='"+this.textBox5.Text.Trim()+"' and ";
if(this.textBox6.Text.Trim()!="")
selstr+="IP地址='"+this.textBox6.Text.Trim()+"' and ";
if(selstr.Length>0)//存在查询条件
{
selstr=selstr.Substring(0,selstr.Length-5);//这句意思是从selstr中减去最后5个字符
//因为有" and "
}
this.dv.RowFilter=selstr;//把selstr赋给DataView对象dv的RowFilter属性筛选出结果
this.textnull();
}
private void textnull()//清空查询文本框
{
this.textBox6.Text="";
this.textBox5.Text="";
this.textBox4.Text="";
this.textBox3.Text="";
this.textBox2.Text="";
this.textBox1.Text="";
}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==(char)13)//检测是否按下了回车键
{
this.button1.Focus();//得到焦点
}
}
}
}
[此贴子已经被作者于2005-12-19 15:10:09编辑过]