命名规范不只是增加可读性!(实例:asp.net C#学生信息管理系统)

时间:2021-04-05 00:58:35

一直以来,对于我们初学者,很难意识到命名规范的重要性,所以平时我们没有给予足够的重视,一下通过一段代码的解读,向大家展示一下命名规范给编程带来的便利,灵活:

 

代码如下:

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyDll;

public partial class stuInfo : System.Web.UI.Page
{
    string sql = "select * from student";

    #region Page_Load

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillGrid(this.sql);
            initialControlsBind();
        }
    }

    #endregion

    #region 填充DataView

    public void fillGrid(string sql)
    {
        if (ViewState["ds"] == null)
        {
            ViewState["ds"] = DataAccess.getDataSet(sql);
        }
        GridView1.DataSource = ((DataSet)ViewState["ds"]).Tables[0];
        GridView1.DataBind();

    }

    #endregion

    #region 初始化控件

    public void initialControlsBind()
    {
        foreach (Control ctl in SearchPanel.Controls)
        {
            if (ctl.ID == null)
            {
                continue;
            }

            switch (ctl.ID.Substring(0, 2))
            {
                case "tb":
                    ((TextBox)ctl).Text = "";
                    break;
                case "dd":
                    string ddSql = "select * from code_" + ((DropDownList)ctl).ID.Substring(2).ToLower();
                    DataSet ds= DataAccess.getDataSet(ddSql,"sex");
                    ((DropDownList)ctl).DataSource =ds.Tables["sex"];
                    ((DropDownList)ctl).DataTextField ="name";
                    ((DropDownList)ctl).DataValueField ="id";
                    ((DropDownList)ctl).DataBind();
                    break;
                default:
                    break;
            }

        }
    }

    #endregion

    #region 分页

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        fillGrid(sql);
    }

    #endregion

    #region 排序

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sqlWithOrder = "select * from student order by " + e.SortExpression+" asc";
        ViewState["ds"]=null;
        fillGrid(sqlWithOrder);
    }

    #endregion

    #region 动态获得查询sql语句

    public string autoGenerateSql()
    {
        string sqlSearch= "select * from student ";

        string cond = "";

        foreach (Control ctl in SearchPanel.Controls)
        {
            if (ctl.ID == null)
            {
                continue;
            }

            switch (ctl.ID.Substring(0, 2))
            {
                case "tb":
                    cond += cond.StartsWith("where") ? "and " : "where ";
                    cond += ((TextBox)ctl).ID.Substring(2) + " like '%" + ((TextBox)ctl).Text + "%'";
                    break;
                case "dd":
                    cond += cond.StartsWith("where") ? "and " : "where ";
                    cond += ((DropDownList)ctl).ID.Substring(2) + " like '%" + ((DropDownList)ctl).SelectedItem.Text + "%'";
                    break;
                default:
                    break;
            }
        }
          
        return sqlSearch+cond;
    }

    #endregion

    #region 查询

    protected void btSearch_Click(object sender, EventArgs e)
    {
        ViewState["ds"] = null;
        fillGrid(autoGenerateSql());
    }

    #endregion

    #region 动态获得插入sql语句

    public string autoGenerateAddSql()
    {
        string sqlAdd= "insert into student(SNO,SSEX,SNAME,SAGE) values(";

        string value = "";

        foreach (Control ctl in SearchPanel.Controls)
        {
            if (ctl.ID == null)
            {
                continue;
            }

            switch (ctl.ID.Substring(0, 2))
            {
                case "tb":
                    value += "'"+((TextBox)ctl).Text+"'"+",";
                    break;
                case "dd":
                    value += "'"+((DropDownList)ctl).SelectedItem.Text+"'" + ",";
                    break;
                default:
                    break;
            }
        }

        value = value.Remove(value.Length - 1);

        return sqlAdd + value+")";
    }

    #endregion

    #region 插入

    protected void btAdd_Click(object sender, EventArgs e)
    {
        Response.Write("已经插入一条,请点击查询!");
        DataAccess.executeDML(autoGenerateAddSql());
        ViewState["ds"] = null;
        fillGrid(this.sql);
    }

    #endregion

    #region 编辑

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        fillGrid(this.sql);

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sqlUpdate = "update student set SNO=" + "'" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString().Trim() + "'"
                                         + ",SNAME=" + "'" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString().Trim() + "'"
                                          + ",SSEX=" + "'" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim() + "'"
                                          + ",SAGE=" + "'" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString().Trim()+ "'"
                                          +" where SNO="+"'"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'";

        Response.Write("你已经更改了一条记录!");
        DataAccess.executeDML(sqlUpdate);
        GridView1.EditIndex = -1;
        ViewState["ds"] = null;
        fillGrid(this.sql);

    }
    #endregion

    #region 取消

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        ViewState["ds"] = null;
        fillGrid(this.sql);

    }

    #endregion

    #region 删除

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlDelete = "delete from student where SNO="+"'"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'";
        Response.Write("你已经删除了一条记录!");
        DataAccess.executeDML(sqlDelete);
        ViewState["ds"] = null;
        fillGrid(this.sql);
    }

    #endregion
  
  
}

 

重点是为咖啡色的部分:分别是initialControls和autoGenerateAddSql,其实,两者的思想是相同的,就解释一下后者吧,在asp.net中,界面的各个控件,一般这里说的是服务器控件都有自己的名字,如文本框,可能默认的ID是TextBox1...而我在编程的时候取名为tbSNO,等等,其中前两个字符代表控件的类型,后两个字符代表所对应的数据的字段,这样当编程时就可以利用对字符串的截取,分别获得控件类型和对应的数据源的字段,方便了操作,而且当界面和数据库的表发生变化时,只需做极少的修改,基本上不需要修改。

 

可能命名的规范带来的好处不止如此,我们大家在以后编程过程中一定要注意啊!