ASP.NET_数据库数据在网页显示

//html页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="连接数据库.Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>


      <%--  数据绑定控件--%>


      <asp:GridView ID="newlist" runat="server">
         <Columns>


         </Columns>
      </asp:GridView>
    </div>
    </form>
</body>
</html>

===========================================================================

//后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;




using System.IO;


namespace 连接数据库
{
    public partial class Index : System.Web.UI.Page
    {
        DBHelper.DBHelperSQL help = new DBHelper.DBHelperSQL();//连接数据库,我用的是Web.config和DBHelper类连接的,建议你们自己写,不要复制


        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetNewLoad();
            }
        }
        /// <summary>
        /// 方法
        /// </summary>
        public void GetNewLoad() {
            help.Open();
            System.Data.DataSet ds = help.ExecuteSQL(string.Format("SELECT * FROM Student",help.sqlConn));
            //数据绑定
            this.newlist.DataSource = ds;
            this.newlist.DataBind();
            help.Colse();

        }

    }
}


猜你喜欢

转载自blog.csdn.net/lan_se_tian_ma/article/details/80950849
今日推荐