欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-07 08:16:09
一、把连接字符串放到web.config文件中 [html]    

一、把连接字符串放到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,x.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(); 
        } 
    } 

 


作者 yousuosi