Ejemplo n.º 1
0
    private void get_dataTable()
    {
        //连接数据库
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MovieConnectionString"].ConnectionString);

        conn.Open();
        //读取数据库存放到内存:dataSet
        DataSet        ds  = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter("select * from [MovieInfo]", conn);

        sda.Fill(ds, "t");
        //设置分页器
        PagedDataSource pds = new PagedDataSource();

        pds.DataSource  = ds.Tables["t"].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize    = 4;

        //设置访问逻辑代码
        int curPage, count;

        if (Request.QueryString["Page"] != null)
        {
            curPage = Convert.ToInt32(Request.QueryString["Page"]);
        }
        else
        {
            curPage = 1;
        }

        pds.CurrentPageIndex = curPage - 1;
        count = pds.PageCount;
        lblCurrentPage.Text = "当前第:" + curPage.ToString() + "页";
        lblTotalPage.Text   = "总共" + count.ToString() + "页";

        if (!pds.IsFirstPage)
        {
            this.first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
            this.last.NavigateUrl  = Request.CurrentExecutionFilePath + "?Page=" + count.ToString();
            this.up.NavigateUrl    = Request.CurrentExecutionFilePath + "?Page=" + (curPage - 1).ToString();
        }
        else
        {
            this.first.Visible = false;
        }
        if (!pds.IsLastPage)
        {
            this.next.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + (curPage + 1).ToString();
            this.last.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + count.ToString();
        }
        else
        {
            this.last.Visible = false;
        }

        //绑定数据源
        MyRep.DataSource = pds;
        MyRep.DataBind();
        conn.Close();
    }
Ejemplo n.º 2
0
    private void get_dataTable(string id)
    {
        //连接数据库
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyPetConnectionString"].ConnectionString);

        conn.Open();
        //读取数据库存放到内存:dataSet
        DataSet        ds  = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter("select * from [MyPetInfo] where ID=" + id, conn);

        sda.Fill(ds, "t");
        MyRep.DataSource = ds.Tables["t"].DefaultView;
        MyRep.DataBind();
        conn.Close();
    }