如何从数据库读取一系列商品信息并在前台展示。

mori森夏 2014-05-03 02:43:15
前台后台该如何写,新手请教。
...全文
651 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mori森夏 2014-05-04
  • 打赏
  • 举报
回复
引用 5 楼 wind_cloud2011 的回复:
一般在后台都需要有对应的DataBind()在执行绑定。 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDataList(); } } private void BindDataList() { string con=""; string strSql = "SELECT * FROM table";//定义一条SQL语句 SqlDataAdapter sda = new SqlDataAdapter(strSql, con); DataSet ds = new DataSet(); sda.Fill(ds); newproductlist.DataSource = ds; newproductlist.DataBind(); }
谢谢,可以了,我自己再琢磨琢磨
Ny-6000 2014-05-04
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
空洞地说,就是“从数据库读取一系列商品信息并帮定给前台模板展示”。 具体地说,就是“谁知道你会用什么呢?你应该先从你力所能及的简单问题学习,然后自己设计综合在一起的程序”。
有道理,说得太多.是害了人家的.
wind_cloud2011 2014-05-04
  • 打赏
  • 举报
回复
一般在后台都需要有对应的DataBind()在执行绑定。 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDataList(); } } private void BindDataList() { string con=""; string strSql = "SELECT * FROM table";//定义一条SQL语句 SqlDataAdapter sda = new SqlDataAdapter(strSql, con); DataSet ds = new DataSet(); sda.Fill(ds); newproductlist.DataSource = ds; newproductlist.DataBind(); }
mori森夏 2014-05-04
  • 打赏
  • 举报
回复
引用 2 楼 wind_cloud2011 的回复:

给你一个简单的示例,读取数据库在前面GridView中显示。
后台读取数据库代码
  public string connectionString = "Data Source=192.168.1.2;Initial Catalog=data;User ID=sa;password=sa;Integrated Security=False";
        public string queryString = "select * from table";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    SqlCommand command = new SqlCommand(queryString, connection);
                    connection.Open();
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                    command.Dispose();
                    connection.Close();
                 }
            }

        }

前台的加入控件GridView1,就可
 <form id="form1" runat="server">
    <div>    
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>    
    </div>
    </form>

其实我就是想问比如说一下代码
<asp:DataList runat="server" ID="newproductlist" RepeatColumns="5" RepeatDirection="Horizontal" CellSpacing="5">
            <ItemTemplate>
                <div class="goods_show">
                    <a target="_self" href="#">
                    <img src="image/<%Eval("Gimage") %>>" width="180" height="180" />
                    </a>
                <div class="brief_info"><%Eval("Gname")%> <b>¥<%Eval("Gprice")%></b></div>
                </div>
            </ItemTemplate>
    </asp:DataList>
后台除了连接数据库,还需要什么操作的?
wind_cloud2011 2014-05-03
  • 打赏
  • 举报
回复

给你一个简单的示例,读取数据库在前面GridView中显示。
后台读取数据库代码
  public string connectionString = "Data Source=192.168.1.2;Initial Catalog=data;User ID=sa;password=sa;Integrated Security=False";
        public string queryString = "select * from table";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    SqlCommand command = new SqlCommand(queryString, connection);
                    connection.Open();
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                    command.Dispose();
                    connection.Close();
                 }
            }

        }

前台的加入控件GridView1,就可
 <form id="form1" runat="server">
    <div>    
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>    
    </div>
    </form>

  • 打赏
  • 举报
回复
空洞地说,就是“从数据库读取一系列商品信息并帮定给前台模板展示”。 具体地说,就是“谁知道你会用什么呢?你应该先从你力所能及的简单问题学习,然后自己设计综合在一起的程序”。

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧