like的左右是要加%的,如果没有,那跟精确查询有什么分别?改称这样试试看: string SRCsql="select name,class,workname,email,workphone,mobilephone,address from student where(name like '%"+txtSearch.Text+"%' or class like '%"+txtSearch.Text+"%' or workname like '%"+txtSearch.Text+"%' or email like '%"+txtSearch.Text+"%' or workphone like '%"+txtSearch.Text+"%' or mobilephone like '%"+txtSearch.Text+"%' or address like '%"+txtSearch.Text+"%')"; 还有:你这个数据源绑定SqlDataReader,这行吗?
我现在代码改成这样了还是不行.... private void btnSearch_Click(object sender, System.EventArgs e) { String Keyword = "'%"+txtSearch.Text+"%'"; String SRCSql = "SELECT name,class,workname,email,workphone,mobilephone,address FROM student WHERE (name LIKE @Keyword OR class LIKE @Keyword OR workname LIKE @Keyword OR email LIKE @Keyword OR workphone LIKE @Keyword OR mobilephone LIKE @Keyword OR address LIKE @Keyword)"; SqlCommand srcCommand = new SqlCommand(SRCSql, studentConnection); srcCommand.Parameters.Add("@Keyword", SqlDbType.NVarChar).Value = Keyword; SqlDataAdapter daStudent = new SqlDataAdapter(srcCommand); DataTable tableStudent = new DataTable(); daStudent.Fill(tableStudent); studentDataGrid.DataSource = tableStudent; studentDataGrid.DataBind(); }
string SRCsql="select name,class,workname,email,workphone,mobilephone,address from student where(name like '%"+txtSearch.Text+"%' or class like '%"+txtSearch.Text+"%' or workname like '%"+txtSearch.Text+"%' or email like '%"+txtSearch.Text+"%' or workphone like '%"+txtSearch.Text+"%' or mobilephone like '%"+txtSearch.Text+"%' or address like '%"+txtSearch.Text+"%')";
还有:你这个数据源绑定SqlDataReader,这行吗?
查看 srcdr 是否为空
-------------------------------
可以以datareader为datasource。怀疑是楼主的SQL语句查询结果是空
==================
呵呵,我刚才还向问你这个问题呢,都打出来了,一想算了,在别人的帖子上这么问,多不好啊,就没发上去。嘿,没想到,你居然告诉我了,唉!可惜你不是个女的,要不然...
---------------------------
用dataadapter.fill
public DataTable DataReaderToDataTable(SqlDataReader dataReader)
{
DataTable datatable = new DataTable();
DataTable schemaTable = dataReader.GetSchemaTable();try
{
foreach(DataRow myRow in schemaTable.Rows)
{
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType = myRow[0].GetType();
myDataColumn.ColumnName = myRow[0].ToString();
datatable.Columns.Add(myDataColumn);
}while(dataReader.Read())
{
DataRow myDataRow = datatable.NewRow();for(int i=0;i{
myDataRow[i] = dataReader[i];
}
datatable.Rows.Add(myDataRow);
myDataRow = null;
}
schemaTable = null;
dataReader.Close();
return datatable;
}
catch(Exception ex)
{
throw new Exception("error",ex);
}
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
String Keyword = "'%"+txtSearch.Text+"%'";
String SRCSql = "SELECT name,class,workname,email,workphone,mobilephone,address FROM student WHERE (name LIKE @Keyword OR class LIKE @Keyword OR workname LIKE @Keyword OR email LIKE @Keyword OR workphone LIKE @Keyword OR mobilephone LIKE @Keyword OR address LIKE @Keyword)";
SqlCommand srcCommand = new SqlCommand(SRCSql, studentConnection);
srcCommand.Parameters.Add("@Keyword", SqlDbType.NVarChar).Value = Keyword;
SqlDataAdapter daStudent = new SqlDataAdapter(srcCommand);
DataTable tableStudent = new DataTable();
daStudent.Fill(tableStudent);
studentDataGrid.DataSource = tableStudent;
studentDataGrid.DataBind();
}