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


    源代码:13033480群共享

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

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

    二、添加类库Model,设置类库项目的程序集名称和默认命名空间

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

    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;代码如下:

    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();
            }
        }
    }
    

    版权所有©2012,西园电脑工作室.欢迎转载,转载请注明出处.更多文章请参阅博客http://blog.csdn.com/yousuosi

  • 相关阅读:
    NYOJ232 How to eat more Banana
    NYOJ716 River Crossing(第六届河南省程序设计大赛)
    HDOJ1847 Good Luck in CET4 Everybody!
    NYOJ234 吃土豆
    HDOJ1850Being a Good Boy in Spring Festival
    HDOJ2176 取(m堆)石子游戏
    NYOJ78 圈水池(简单凸包)
    POJ1656 Counting Black (二维树状数组)
    HDOJ1892 See you~(二维树状数组)
    取石子问题 – 1堆
  • 原文地址:https://www.cnblogs.com/WestGarden/p/3138435.html
Copyright © 2020-2023  润新知