当前位置:编程学习 > asp >>

网上购物系统(Task001)——公共数据集Model

一、把连接字符串放到Web.config文件中

[html] <connectionStrings> 
  <add name="NetShopConnString" connectionString="server=.\SQLEXPRESS;database=NetShop;integrated security=SSPI;" providerName="System.Data.SqlClient"/> 
</connectionStrings> 
  <connectionStrings>
    <add name="NetShopConnString" connectionString="server=.\SQLEXPRESS;database=NetShop;integrated security=SSPI;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

二、添加类库Model。

三、Model中添加类CategoryInfo.cs,代码如下:

[csharp] using System; 
using System.Collections.Generic; 
using System.Text; 
 
namespace WestGarden.Model 

    public class CategoryInfo 
    { 
        private string categoryid; 
        private string name; 
        private string descn; 
 
        public CategoryInfo() { } 
 
        public CategoryInfo(string categoryid, string name, string descn) 
        { 
            this.categoryid = categoryid; 
            this.name = name; 
            this.descn = descn; 
        } 
 
        public string CategoryId 
        { 
            get { return categoryid; } 
        } 
        public string Name 
        { 
            get { return name; } 
        } 
        public string Descn 
        { 
            get { return descn; } 
        } 
    } 

using System;
using System.Collections.Generic;
using System.Text;

namespace WestGarden.Model
{
    public class CategoryInfo
    {
        private string categoryid;
        private string name;
        private string descn;

        public CategoryInfo() { }

        public CategoryInfo(string categoryid, string name, string descn)
        {
            this.categoryid = categoryid;
            this.name = name;
            this.descn = descn;
        }

        public string CategoryId
        {
            get { return categoryid; }
        }
        public string Name
        {
            get { return name; }
        }
        public string Descn
        {
            get { return descn; }
        }
    }
}
 

四、Web中添加引用→项目→Model,Defualt,aspx.cs中添加using WestGarden.Model;代码如下:

[csharp] using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Collections.Generic; 
using WestGarden.Model; 
 
namespace WestGarden.Web 

    public partial class Default : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            IList<CategoryInfo> catogories = new List<CategoryInfo>(); 
 
            string connectionString = ConfigurationManager.ConnectionStrings["NetShopConnString"].ConnectionString; 
            string cmdText = "SELECT * FROM Category"; 
 
            SqlConnection conn = new SqlConnection(connectionString); 
 
            SqlCommand cmd = new SqlCommand(); 
            cmd.Connection = conn; 
            cmd.CommandType = CommandType.Text; 
            cmd.CommandText = cmdText; 
 
            conn.Open(); 
 
            SqlDataReader rdr = cmd.ExecuteReader(); 
            while (rdr.Read()) 
            { 
                CategoryInfo category=new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2)); 
                catogories.Add(category); 
            } 
 
            rdr.Close(); 
            conn.Close(); 
 
            ddlCategories.DataSource = catogories; 
            ddlCategories.DataTextField = "Name"; 
            ddlCategories.DataValueField = "CategoryId"; 
            ddlCategories.DataBind(); 
        } 
&nbs

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,