A student who doesn't want to be a general is not a good programmer-Thinking about the data access layer Dal-program structure

Source: Internet
Author: User

Source code: 13033480 group sharing

Guo Degang said: "The cook who doesn't want to be a tailor is not a good driver ".

 

Does he mean this?

 

I can cook delicious food at home every day (if you think of a cook as cooking for others, I can't help it );

I can afford beautiful clothes. (If you think of a tailor as tailored to others, I can't help it );

Another person driving a BMW-Benz ride (if you think of the driver as someone else who is trying to drive a broken car, then I can't help it );

In order to win the favor of beautiful women.

 

Oh, the benevolent sees benevolence, I, that's my IQ...

 

I am not a good programmer who doesn't want to be a general student:

 

GENERAL: it is an idea to have a systematic point of view, to direct others to do things, and not to do anything;

Student: What is the highest level of students? It's a bachelor's degree...

There is no big difference between "Bachelor's degree" and "warrior". It's just a war professional, a learning professional...

Programmer: is it not a good programmer for a computer science student, a bachelor who has a systematic viewpoint ??

 

We have already worked as a tailor, tailored to create a dataset model. We have encapsulated the core classes of ADO. Net into sqlhelper and constructed a function set dbutilty.

Now, it's time for us to set up a functional department and hand over all the basic dirty and tiring jobs to them?

 

By the way, this function department is called the data access layer dal...

 

Procedure]

1. Add the class library Dal, add the class category. CS to the Dal, and add the function getcategories () to the class library. The Code is as follows:

using System;using System.Data;using System.Collections.Generic;using System.Text;using System.Data.SqlClient;using WestGarden.Model;using WestGarden.DBUtility;namespace WestGarden.DAL{    public class Category    {        private const string SQL_SELECT_CATEGORIES = "SELECT CategoryId, Name, Descn FROM Category";        public IList<CategoryInfo> GetCategories()        {            IList<CategoryInfo> categories = new List<CategoryInfo>();            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORIES, null))            {                while (rdr.Read())                {                    CategoryInfo cat = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2));                    categories.Add(cat);                }            }            return categories;        }    }}

The getcategories () function reads the connection string in sqlhelper. Therefore, you need to add the code in sqlhelper. CS:

Public static readonly string connectionstringlocaltransaction = configurationmanager. connectionstrings ["netshopconnstring"]. connectionstring;

 

Note: Add reference system. configuration.

2. Use User Controls

Create a folder controls on the web and add the user control navigationcontrol. ascx to it. The code for the form page and the code page are as follows:

1. navigationcontrol. ascx

 

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="NavigationControl.ascx.cs" Inherits="WestGarden.Web.NavigationControl" %><%@ OutputCache Duration="100000" VaryByParam="*" %><asp:Repeater ID="repCategories" runat="server"><HeaderTemplate><table cellspacing="0" border="0" style="border-collapse: collapse;"></HeaderTemplate><ItemTemplate><tr><td class="<%= ControlStyle %>"><asp:HyperLink runat="server" ID="lnkCategory"  NavigateUrl='<%# string.Format("~/Items.aspx?page=0&categoryId={0}", Eval("CategoryId")) %>' Text='<%# Eval("Name") %>' /><asp:HiddenField runat="server" ID="hidCategoryId" Value='<%# Eval("CategoryId") %>' /></td></tr></ItemTemplate><FooterTemplate></table></FooterTemplate></asp:Repeater>

 

2. navigationcontrol. ascx. CS

using System;using System.Web.UI.WebControls;using WestGarden.DAL;namespace WestGarden.Web {    public partial class NavigationControl : System.Web.UI.UserControl {                       private string controlStyle;        protected string ControlStyle {            get { return controlStyle; }        }        protected void GetControlStyle() {            if (Request.ServerVariables["SCRIPT_NAME"].ToLower().IndexOf("default.aspx") > 0)                controlStyle = "navigationLinks";            else                controlStyle = "mainNavigation";        }        protected void Page_Load(object sender, EventArgs e) {            GetControlStyle();            BindCategories();            string categoryId = Request.QueryString["categoryId"];            if (!string.IsNullOrEmpty(categoryId))                SelectCategory(categoryId);        }        private void SelectCategory(string categoryId) {            foreach (RepeaterItem item in repCategories.Items) {                HiddenField hidCategoryId = (HiddenField)item.FindControl("hidCategoryId");                if(hidCategoryId.Value.ToLower() == categoryId.ToLower()) {                    HyperLink lnkCategory = (HyperLink)item.FindControl("lnkCategory");                    lnkCategory.ForeColor = System.Drawing.Color.FromArgb(199, 116, 3);                    break;                }            }        }        private void BindCategories() {            Category category = new Category();            repCategories.DataSource = category.GetCategories();            repCategories.DataBind();                    }    }}

 

The three parameters are westult.aspx.pdf theme westgardenand the sample table stylesheet.css. Apply the theme in default. aspx, delete the code in the default. aspx. CS code page and the reapeter control in default. aspx, and drag the user control into the page directly.

 

[Notes]

1. the user control navigationcontrol has different styles because it needs to be applied on the home page and master page. Therefore, the getcontrolstyle () function (), determine whether the address contains the default value. aspx to determine the style used.

2. In the wizard bar on the master page, click category items, and the color of the corresponding category items will change. Therefore, with the selectcategory (string categoryid) function, you can use the categoryid obtained in the address bar to query the categoryid pre-placed in the hiddenfield and obtain the category items of the corresponding Repeater control, and set the corresponding hyperlink color.

3. We set ADO.. net core classes are encapsulated in sqlhelper. in CS, you only need to tell it which connection, what the command type is, what the command content is, what the command parameters are, and then you can use the universal database to access the function, complete basic query, update, insert, and delete operations.

4. We encapsulate basic query, update, insert, and delete functions in the data access layer Dal, that is, the basic parameters for accessing the database: the connection, command type, command content, and command parameters are all stored in the corresponding functions of the data access layer. In this way, the upper layer is in the application layer of our current two-layer structure, instead of connecting and Executing command operations, or configuring connection strings and command content, you only need to simply publish a command and call a getcategories () function, everything is okay. Are we from slaves to generals ????

In addition, we sent the meeting to ADO. the net core class is sent to sqlhelp in the class library dbutility. in the CS class, this time, the code of the function segment for querying the name field of the product category table will be directly formed by getcategories (), which is directly placed on the data access layer without sending an October, it is only to prevent programmers from having any lover, and again the reason for being too sad, I would like to explain...

Copyright 2012, westgarden. Welcome to reprint, reprint please indicate the source. For more articles please refer to blog http://www.cnblogs.com/WestGarden/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.