编写搜索按钮的问题
各位高手,能不能帮我给忙,帮我看看这段代码有没有问题。如果我是想要编一个员工窗体的搜索按钮,(根据员工编号和员工姓名来搜索)并把搜索结果显示在相应的控件里面,或者在DataGridView里面只显示所搜索的那一行,要怎么编呢?下面的代码还要怎么改???
private void btnSearch_Click(object sender, EventArgs e)
{
SqlExcuClass objSqlExcuClass=new SqlExcuClass ();
string Sql;
Sql = "select StoreKeepersID AS 库管员编号,StoreKeepersName AS 姓名,Sex AS 性别,IdentityID AS 身份证号,Birthday AS 出生日期,EnterLibraryDate AS 入馆日期,Phone AS 电话,Address AS 地址 from StoreKeepersInfo";
switch (cobSearchTerms.SelectedIndex)
{
case 1:
Sql += "where StoreKeepersID='" + txtSearchTerms.Text.Trim() + "'";
break ;
case 2:
Sql += "where StoreKeepersName='" + txtSearchTerms.Text.Trim() + "'";
break;
}
DataSet sqldataset = objSqlExcuClass.getds(Sql, "StoreKeepersInfo");
dataGridView1.DataSource =
}
}
下面是SqlExcuClass类里的内容
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace LibraryManagementSystem
{
class SqlExcuClass
{
//建立数据库连接
public SqlConnection getsqlconn()
{
string connstring = "SERVER=(local)\\SQLEXPRESS;Database=LibraryManagementSystem;Integrated Security=SSPI";
SqlConnection sqlconn = new SqlConnection(connstring);
return sqlconn;
}
//执行插入、删除、修改等无返回值的语句:使用Sqlcommand的ExecuteNonQuery()方法;
public void getnonqerycom(string sql)
{
SqlConnection sqlconn = this.getsqlconn();
sqlconn.Open();
SqlCommand sqlcom = new SqlCommand(sql, sqlconn);
sqlcom.ExecuteNonQuery();
sqlcom.Dispose();
sqlconn.Close();
}
//执行查询语句,且需一个一个返回值:使用Sqlcommand的ExecuteReader()方法,赋值给SqlDataReader对象;
public SqlDataReader getread(string sql)
{
SqlConnection sqlconn = this.getsqlconn();
sqlconn.Open();
SqlCommand sqlcom = new SqlCommand(sql, sqlconn);
SqlDataReader sqldr = sqlcom.ExecuteReader(CommandBehavior.CloseConnection);
return sqldr;
}
//执行查询语句,一下子接受所有信息:使用DataSet对象
public DataSet getds(string sql, string seach_table)
{
SqlConnection sqlconn = this.getsqlconn(); //Fill会自动打开连接,并处理后自动关闭自身打开的连接,若连接已经打开,Fill处理后,连接仍是打开的。
SqlDataAdapter sqlda = new SqlDataAdapter(sql, sqlconn);
DataSet sqlds = new DataSet();
sqlda.Fill(sqlds, seach_table);
return sqlds;
}
}
}
[ 本帖最后由 荨夕泪 于 2012-1-3 14:39 编辑 ]