Asp.Net使用DataSet查询数据库数据

DAL层
using System;
using System.Collections.Generic;
using System.Text;
using MODEL;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public static class ArticleService
    {
        //dateset方法
        public static DataSet GetDateSet()
        {
            string strsql = "select * from t_Article";
            try
            {
                DataSet ds = DBHelper.GetDataSet(strsql);
                return ds;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
    }
}
BLL层
using System;
using System.Collections.Generic;
using System.Text;
using MODEL;
using DAL;
using System.Data;

namespace BLL
{
    public class ArticleManager
    {
        //用DataSet查询
        public static DataSet GetDateSet()
        {
            return ArticleService.GetDateSet();
        }
    }
}
MODEL层
using System;
using System.Collections.Generic;
using System.Text;

namespace MODEL
{
    public class Article
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string title;

        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        private string content;

        public string Content
        {
            get { return content; }
            set { content = value; }
        }
        private DateTime time;

        public DateTime Time
        {
            get { return time; }
            set { time = value; }
        }
        private string url;

        public string Url
        {
            get { return url; }
            set { url = value; }
        }
    }
}
WEB层
show.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="show.aspx.cs" Inherits="show" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging">
        </asp:GridView>
        <div>
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                    </td>
                </tr>
            </table>
        </div>
    </form>
    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</body>
</html>
show.aspx.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using MODEL;
using System.Collections.Generic;
using System.Data.SqlClient;

public partial class show : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
     /*************************************************************************************/
            //用DataSet获得数据集(普通的显示)
            //DataSet ds = ArticleManager.GetDateSet();
            //Label1.Text = ds.Tables[0].Rows[0]["t_ID"].ToString();
            //Label2.Text = ds.Tables[0].Rows[0]["t_Title"].ToString();
            //Label3.Text = ds.Tables[0].Rows[0]["t_Content"].ToString();

            //用DataSet获得数据集(循环获得数据库数据)
            //DataSet ds = ArticleManager.GetDateSet();
            //foreach (DataRow row in ds.Tables[0].Rows)
            //{
            //    string id = row["t_ID"].ToString();
            //    string title = row["t_Title"].ToString();
            //    string content = row["t_Content"].ToString();
            //    string datae = row["t_Date"].ToString();


            //    Label lbl1 = new Label();
            //    Label lbl2 = new Label();
            //    lbl1.Text = id.ToString();
            //    lbl2.Text = title;
            //    Label1.Text += lbl1.Text + "<br>";
            //    Label2.Text += lbl2.Text + "<br>";
            //}

            //GridView控件用DataSet绑定
            //DisplayDataSet();
            /*************************************************************************************/
 }
    }
    //DataSet绑定
    public void DisplayDataSet()
    {
        //可以使用
        //DataSet ds = ArticleManager.GetDateSet();
        //GridView1.DataSource = ds.Tables[0];

        GridView1.DataSource = ArticleManager.GetDateSet();
        GridView1.DataBind();
    }
    //分页时调用
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //DataSet绑定
        GridView1.PageIndex = e.NewPageIndex;
        DisplayDataSet();
    }
}

你可能感兴趣的:(数据库,String,server,asp.net,asp,dataset)