.Net Project/ADO.NET 3.5

12장 ADO.NET 메모리상의 데이터 베이스(RowFilter)

래곤 2009. 9. 28. 15:24
반응형

 RowFilter : 데이터 검색시 사용하는 속성 : "CategoryName" Like '%컴퓨터%'"
        Connection -> Command -> DataAdapter -> DataSet ->
        DataTable -> DataView -> DataView.RowFilter 속성

DataView에 표시할 행을 필터링하는 데 사용하는 식을 가져오거나 설정


using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class Category_FrmRowfilter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //커넥션
        SqlConnection con = new SqlConnection
                          (ConfigurationManager.ConnectionStrings
                           ["ConnectionString"].ConnectionString);
        con.Open();

        //커멘드
        SqlCommand cmd = new SqlCommand
                    ("Select *From Categories", con);
       
        cmd.CommandType = CommandType.Text;

        //어댑터
        SqlDataAdapter da = new SqlDataAdapter(cmd); //초기화
        DataSet ds = new DataSet();
        da.Fill(ds, "Categories");

        //전체출력
        this.ctlSearchList.DataSource = ds;
        this.ctlSearchList.DataBind();
        
        con.Close();
      
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection
                     (ConfigurationManager.ConnectionStrings
                      ["ConnectionString"].ConnectionString);
        con.Open();

        SqlCommand cmd = new SqlCommand
                   ("Select *From Categories", con);
        cmd.CommandType = CommandType.Text;

        SqlDataAdapter da = new SqlDataAdapter(cmd); //초기화
        DataSet ds = new DataSet();
        da.Fill(ds, "Categories");

        //전체출력 
        this.ctlSearchList.DataSource = ds;
        this.ctlSearchList.DataBind();

       
        //[1]DataTable변환
        DataTable dt = ds.Tables[0];

        //[2]DataView 변환
        DataView dv = dt.DefaultView;

        //[3]DataView.RowFilter변경
        dv.RowFilter = "CategoryName Like '%" + txtSerchquery.Text + "%'";
        dv.Sort = "CategoryName Asc"; //정렬

        this.ctlSearchList.DataSource = dv;
        this.ctlSearchList.DataBind();
       
        con.Close();
   }
}






반응형