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

本文介绍了一个基于C#的分层架构示例,包括DAL(数据访问层)、BLL(业务逻辑层)、MODEL(模型层)和WEB(表现层)。通过具体的代码实现了文章数据的获取与展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
    {
 /*************************************************************************************/
        //datatable方法
        public static DataTable GetDataTable()
        {
            string strsql = "select * from t_Article";
            try
            {
                DataTable dt = DBHelper.GetDataTable(strsql);
                return dt;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        //public static void a()
        //{
        //    SqlConnection conn = new SqlConnection("server=AA296D9D5D2B411;database=accp;uid=sa;pwd=accp;");
        //    string strsql = "select * from test where name=" + name;
        //    SqlDataAdapter sda = new SqlDataAdapter(strsql, conn);
        //    DataTable dt = new DataTable();
        //    conn.Open();
        //    sda.Fill(dt);
        //    Label1.Text = dt.Rows[0]["age"].ToString();
        //}
    }
}
BLL层
using System;
using System.Collections.Generic;
using System.Text;
using MODEL;
using DAL;
using System.Data;

namespace BLL
{
    public class ArticleManager
    {
        //用DataTable查询
        public static DataTable GetDataTable()
        {
            return ArticleService.GetDataTable();
        }
    }
}

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)
        {
     /*************************************************************************************/
            //用DataTable获得数据集(普通的显示)
            //DataTable dt = ArticleManager.GetDataTable();
            //Label1.Text = dt.Rows[0]["t_ID"].ToString();
            //Label2.Text = dt.Rows[0]["t_Title"].ToString();
            //Label3.Text = dt.Rows[0]["t_Content"].ToString();

            //用DataTable获得数据集(普通的显示)
            //DataTable d = ArticleManager.GetDataTable();
            //foreach (DataRow row in d.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;
            //    lbl2.Text = title;
            //    Label1.Text += lbl1.Text + "<br>";
            //    Label2.Text += lbl2.Text + "<br>";
            //}

            //GridView控件用DateTable绑定
            //DisplayDateTable();
            /*************************************************************************************/
        }
 }
    }
    //DataTable绑定
    public void DisplayDateTable()
    {
        DataTable dt = ArticleManager.GetDataTable();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    //分页时调用
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //DataSet绑定
        GridView1.PageIndex = e.NewPageIndex;
        DisplayDateTable();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值