关于gridview 实现查询功能的方法(转载)

protected void btnSearch_Click(object sender, EventArgs e)
{
TestCon();
}
protected void btnAllData_Click(object sender, EventArgs e)
{
TestConAll();

}
private void TestConAllData()
{
string strConn = "data source=.;initial catalog=JLCallSystem;user id=sa;password=123456";

SqlConnection con = new SqlConnection(strConn);
DataSet ds = new DataSet();
con.Open();
string cmdtext = "select * from tb_Customer_Type where intID = '"+hf_UserID.Value+"'";
//数据库记录保存到字符串
//创建SqlDataAdapter数据适配器
SqlDataAdapter sda = new SqlDataAdapter(cmdtext, con);
//创建数据集
//填充数据集合(如下:填充进字符串表名Master)
sda.Fill(ds, "Master");
GridView1.DataSource = ds;

GridView1.DataBind();
}

#region 连接数据库

private void ExcuteSql(String strSql)
{


//string strConn = "Data Source = .;initial Catalog = JLCallSystem;User ID = sa;Password=123456";
string strConn = "Data Source = .;initial Catalog = JLCallSystem;User ID = sa;Password=123456";
DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");
DbConnection dbConn = dbProviderFactory.CreateConnection();
dbConn.ConnectionString = strConn;
dbConn.Open();
DbCommand dbComm = dbProviderFactory.CreateCommand();
dbComm.Connection = dbConn;
dbComm.CommandText = strSql;
dbComm.ExecuteNonQuery();
dbConn.Close();
dbConn.Dispose();

 


}
#endregion

private void TestCon()
{
string strConn = "data source=.;initial catalog=JLCallSystem;user id=sa;password=123456";

SqlConnection con = new SqlConnection(strConn);
DataSet ds = new DataSet();
con.Open();
//自定义查询SQL字符串
string type = this.txtstrType.Text; //需要查寻的数据,从TextBox中读取

string cmdtext = "select * from tb_Customer_Type where strTypeValue like '%" + type + "%'";
//string cmdtext = "select * from users where username like '%"+strTemp+"%'and phone1 like'%"+txtPhone.Text+"%'and phone2 like '%"+txtPhone.Text+"%'and phone3 like '%"+txtPhone.Text+"'and phone4 like'%"+txtPhone.Text+"%'and phone5 like '%"+txtPhone.Text +"%'";

//数据库记录保存到字符串
//创建SqlDataAdapter数据适配器
SqlDataAdapter sda = new SqlDataAdapter(cmdtext, con);
//创建数据集
//填充数据集合(如下:填充进字符串表名Master)
sda.Fill(ds, "Master");
GridView1.DataSource = ds;
GridView1.DataBind();
}

 


#region 数据绑定
private void BindData()
{
string strConn = "data source=.;initial catalog=JLCallSystem;user id=sa;password=123456";

SqlConnection con = new SqlConnection(strConn);
DataSet ds = new DataSet();
con.Open();
//自定义查询SQL字符串
string username = txtstrType.Text.Trim(); //需要查寻的数据,从TextBox中读
string cmdtext = "select * from tb_Customer_Type";
//数据库记录保存到字符串
//创建SqlDataAdapter数据适配器
SqlDataAdapter sda = new SqlDataAdapter(cmdtext, con);
//创建数据集
//填充数据集合(如下:填充进字符串表名Master)
sda.Fill(ds, "Master");
GridView1.DataSource = ds;
GridView1.DataBind();

}
#endregion

private void TestConAll()
{
string strConn = "data source=.;initial catalog=JLCallSystem;user id=sa;password=123456";

SqlConnection con = new SqlConnection(strConn);
DataSet ds = new DataSet();
con.Open();
string cmdtext = "select * from tb_Customer_Type where intID = '"+hf_UserID.Value+"' ";
//数据库记录保存到字符串
//创建SqlDataAdapter数据适配器
SqlDataAdapter sda = new SqlDataAdapter(cmdtext, con);
//创建数据集
//填充数据集合(如下:填充进字符串表名Master)
sda.Fill(ds, "Master");
GridView1.DataSource = ds;
GridView1.DataBind();
}

posted @ 2014-06-19 10:51  大白驴  阅读(1998)  评论(0编辑  收藏  举报