我写的datagrid显示数据库的列子。能成功的翻页、排序等。现在我想实现查询功能,在datagrid的页面中用textbox输入查询条件,再传给下列函数中的sql语句。该语句是下列中的OracleCommand cmd=new OracleCommand("Select * from yonghu",conn);这一句,将Select * from yonghu改为Select * from yonghu where xingming >"+textboxxingming.text+" 。可是却无法将textboxxingming.text这个变量传入,报的错误好象是说只能用静态的变量。请问如何将textboxxingming.text的值传给下面函数中的SQL语句?
public static DataSet GetCustomersData() //好象该static是错误的根源,但又不能改为void。有办法吗?谢谢了!
{
string strConn="Data Source=zhly;User Id=system;Password=wcykzhly";
OracleConnection conn = new OracleConnection();
conn.ConnectionString=strConn;
conn.Open();
//Response.Write("<script language=javascript> alert(成功连接到数据库!); </script>");
OracleCommand cmd=new OracleCommand("Select * from yonghu",conn);
//cmd.Connection=conn;
//cmd.CommandText="Select * from yonghu";
////OracleDataReader myReader = cmd.ExecuteReader();
OracleDataAdapter da=new OracleDataAdapter(cmd);
//da.SelectCommand =cmd;
DataSet ds=new DataSet();
da.Fill(ds);
conn.Close();
return ds;
//DataGridYongHu.DataSource=ds.Tables["yonghu"].DefaultView;
//DataGridYongHu.DataBind();
/*SqlConnection conn = new SqlConnection(connString);
string sqlStr = "SELECT CustomerID, CompanyName,Address,Phone FROM Customers";
SqlCommand comm = new SqlCommand( sqlStr ,conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds; */
}
绑定的语句如下:
private void DataGridDataBind()
{
DataSet ds = GetCustomersData();
recordCount = ds.Tables[0].Rows.Count;
//获取当前的页数
pageCount = (int)Math.Ceiling( recordCount * 1.0 / PageSize);
//避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex > PageCount出错
if(recordCount ==0)
{
this.DataGrid1.CurrentPageIndex = 0;
}
else if(this.DataGrid1.CurrentPageIndex >= pageCount)
{
this.DataGrid1.CurrentPageIndex = pageCount - 1;
}
//this.DataGrid1.DataSource = ds;
ds.Tables[0].DefaultView.Sort=SortField.Text + SortType.Text;//排序方式,后加的
this.DataGrid1.DataSource = ds.Tables[0].DefaultView;//原来语句是this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
NavigationStateChange();
//DataGrid1.SortCommand
}
public static DataSet GetCustomersData()是静态,所以不能直接引用实例化的textboxxingming对象,但是可能使用参数,比如你可以修改GetCustomersData的声明,增加一个参数,调用这个方法将textboxxingming.text传进去.
你在一个静态方法里面引用了实例所以会出现错误,你可以使用下面的函数签名实现:
public static DataSet GetCustomersData(string boxxingming)//将boxxingming的Text传进去即可。