我的第一個ASP.NET項目總結

我的第一個ASP.NET項目總結

最近期末比較忙,沒時間更新博客,下面我將對我自己做的一個ASP.NET項目(設備管理系統)進行總結,頁面模板用的是母版,這樣大大減少了代碼量。最後在文章底部附上項目源碼及數據庫。

  1. 先看效果吧
    1.1 登錄頁
    在這裏插入圖片描述
    1.2 歡迎頁
    在這裏插入圖片描述
    1.3 主頁/列表頁
    在這裏插入圖片描述
    1.4 查詢效果(全字段查詢)
    在這裏插入圖片描述
    1.5 詳情頁/新建、修改
    在這裏插入圖片描述

  2. 登錄示例demo
    爲讓大家看的更加清楚,我將連接連接數據庫的操作直接寫在登錄示例裏了。並且考慮數據安全性,我將密碼進行了哈希加密。

/// <summary>
    /// 登錄
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [Obsolete]
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        //【1】 判斷賬號密碼是否爲空
        if (TextBox1.Text.Trim() =="" || TextBox2.Text.Trim() == "")
        {
            //提示賬號密碼不能爲空
            //Response.Write("<script>alert('賬號密碼不能爲空')</script>");
            Label1.Text = "賬號/密碼不能爲空";
        }
        else
        {
            //清空提示語
            Label1.Text = "";

            //創建數據庫連接
            SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=123;database=sbgl_db");
            //打開數據庫連接
            con.Open();
            //通過用戶名查詢用戶密碼
            string strsql = "select Password from Userinfo where UserName='" + TextBox1.Text.Trim()+"'";
            //SqlDataAdapter myda = new SqlDataAdapter(strsql,con);
            SqlDataReader dr = new SqlCommand(strsql,con).ExecuteReader();
            if (dr.Read())
            {
                //對密碼進行SHA1加密
                string TBpwd = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "SHA1");
                if (TBpwd == dr["Password"].ToString())
                {
                    //創建cookie
                    FormsAuthentication.SetAuthCookie(TextBox1.Text, false);
                    Session["username"] = TextBox1.Text.Trim();
                    //登錄成功跳轉頁面
                    Response.Redirect("welcome.aspx");
                }
                else
                {
                    //密碼錯誤
                    Label1.Text = "密碼錯誤";
                }
            }
            else
            {
                //用戶不存在
                Label1.Text = "用戶不存在";
            }


            //關閉數據庫連接
            con.Close();

        }
    }
  1. 功能示例
    下面以一個表的完整過程進行解析。
    3.1 首先我們可以在前面的【主頁/列表頁】的效果中看到當我們選擇左側邊欄的導航時會切換右側的內容區顯示相應的數據列表。那麼這個其實很簡單,一個gridview控件綁定數據源就能實現,下面是核心邏輯代碼:
/// <summary>
    /// 封裝了綁定數據操作
    /// </summary>
    public void BindDates()
    {
        //實例化類
        publicDB pb = new publicDB();
        //查詢sql語句
        string strsql = "select * from Assetinfo";
        //調用BindDate函數
        object set = pb.BindDate(strsql);

        //綁定數據源
        GridView1.DataSource = set;
        GridView1.DataBind();
    }

3.11(全字段)查詢的核心代碼

/// <summary>
    /// 查詢操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butQuery(object sender, EventArgs e)
    {
        string param = TextBox1.Text;
        //string queryStr = "select * from Assetinfo where AssetDesc like '%"+param+"%'";
        string queryStr = "SELECT * FROM Assetinfo WHERE CONCAT(ISNULL(AssetNo,''),ISNULL(AssetDesc,''),ISNULL(AssetType,''),ISNULL(CheckinDate,''),ISNULL(Other,'')) LIKE  '%" + param + "%'";
        DataSet myset = publicDB.ExcuteDataSet(queryStr);
        GridView1.DataSource = myset;
        GridView1.DataBind();
    }

3.12 完整的代碼如下(包括查詢、翻頁、刪除,以及跳轉詳情頁的操作等)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindDates();
        GridView1.DataKeyNames = new string[] { "AssetNo" };
    }

    /// <summary>
    /// 查詢操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butQuery(object sender, EventArgs e)
    {
        string param = TextBox1.Text;
        //string queryStr = "select * from Assetinfo where AssetDesc like '%"+param+"%'";
        string queryStr = "SELECT * FROM Assetinfo WHERE CONCAT(ISNULL(AssetNo,''),ISNULL(AssetDesc,''),ISNULL(AssetType,''),ISNULL(CheckinDate,''),ISNULL(Other,'')) LIKE  '%" + param + "%'";
        DataSet myset = publicDB.ExcuteDataSet(queryStr);
        GridView1.DataSource = myset;
        GridView1.DataBind();
    }

    /// <summary>
    /// 翻頁操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    /// <summary>
    /// 刪除操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //sql語句字符串
        string delete_sql = "delete from Assetinfo where AssetNo='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

        bool delete = ExceSQLs(delete_sql);

        if (delete)
        {
            //重定向刷新頁面
            Response.Write("<script>alert('刪除成功!');window.location.href = window.location.href </script>");
        }
        else
        {
            Response.Write("<script>alert('刪除失敗!')</script>");
        }
    }

    /// <summary>
    /// 封裝了綁定數據操作
    /// </summary>
    public void BindDates()
    {
        //實例化類
        publicDB pb = new publicDB();
        //查詢sql語句
        string strsql = "select * from Assetinfo";
        //調用BindDate函數
        object set = pb.BindDate(strsql);

        //綁定數據源
        GridView1.DataSource = set;
        GridView1.DataBind();
    }

    /// <summary>
    /// 封裝了傳遞sql語句調用ExceSQL執行sql語句的方法
    /// </summary>
    /// <param name="sqlStr">要執行的sql語句</param>
    /// <returns>返回sql語句執行的狀態,即成功與否</returns>
    public bool ExceSQLs(string sqlStr)
    {
        //實例化類
        publicDB pb = new publicDB();
        //調用ExceSQL執行sql語句返回bool值(即sql執行成功與否)
        bool sqtState = pb.ExceSQL(sqlStr);
        return sqtState;
    }

    /// <summary>
    /// 修改操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = GridView1.DataKeys[e.NewEditIndex]["AssetNo"].ToString();
        Response.Redirect("equipmentUpdate.aspx?AssetNo=" + id);
    }

    /// <summary>
    /// 點擊新建跳轉到編輯詳情頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void toAdd(object sender, EventArgs e)
    {
        Response.Redirect("equipmentAdd.aspx");
    }

    /// <summary>
    /// 刷新操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void refresh(object sender, EventArgs e)
    {
        Response.Write("<script>window.location.href = window.location.href </script>");
    }

    
}

3.2 剛剛前面說到了跳轉詳情,下面介紹詳情頁。
因爲這個項目的字段比較多(28個),因此直接在共一個頁面進行修應該添加操作不美觀,因此我特意添加了一個詳情頁用來完成添加/修改數據的操作。
主要功能就包括,保存、 清空(重置)、新建、返回等功能
代碼如下:
3.2.1 添加操作
Tip: 這裏面用到了一個外部的公共類publicDB,這個類是我自己寫的,封裝了對數據/數據庫的基本操作,如有連接數據、執行增刪改查的基本操作等。

/// <summary>
    /// 保存/更新操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butSave(object sender, EventArgs e)
    {
        //insert sql語句
        string addSql = "insert into Assetinfo(AssetNo,AssetModel,EquipNumber,AssetDesc,AssetType,AssetStat,AssetBooker,StockDate,CheckinDate,YearsOfService,ProduceDate,YearOfUse,SoftwareNo,RejectDate,Unit,EUser,UserPhone,NetConfInfo,Remark,CPU,Mainboard,GraphicsCard,HDisk,NetCard,Memory,CDROM,FloppyDriver,Other) values(@AssetNo,@AssetModel,@EquipNumber,@AssetDesc,@AssetType,@AssetStat,@AssetBooker,@StockDate,@CheckinDate,@YearsOfService,@ProduceDate,@YearOfUse,@SoftwareNo,@RejectDate,@Unit,@EUser,@UserPhone,@NetConfInfo,@Remark,@CPU,@Mainboard,@GraphicsCard,@HDisk,@NetCard,@Memory,@CDROM,@FloppyDriver,@Other)";

        //將所有字段裝到數據裏
        SqlParameter[] prams = new SqlParameter[28];
        prams[0] = new SqlParameter("AssetNo", TextBox1.Text.Trim());
        prams[1] = new SqlParameter("AssetModel", TextBox2.Text.Trim());
        prams[2] = new SqlParameter("EquipNumber", TextBox3.Text.Trim());
        prams[3] = new SqlParameter("AssetDesc", TextBox4.Text.Trim());
        prams[4] = new SqlParameter("AssetType", TextBox5.Text.Trim());
        prams[5] = new SqlParameter("AssetStat", TextBox6.Text.Trim());
        prams[6] = new SqlParameter("AssetBooker", TextBox7.Text.Trim());
        prams[7] = new SqlParameter("StockDate", TextBox8.Text.Trim());
        prams[8] = new SqlParameter("CheckinDate", TextBox9.Text.Trim());
        prams[9] = new SqlParameter("YearsOfService", TextBox10.Text.Trim());
        prams[10] = new SqlParameter("ProduceDate", TextBox11.Text.Trim());
        prams[11] = new SqlParameter("YearOfUse", TextBox12.Text.Trim());
        prams[12] = new SqlParameter("SoftwareNo", TextBox13.Text.Trim());
        prams[13] = new SqlParameter("RejectDate", TextBox14.Text.Trim());
        prams[14] = new SqlParameter("Unit", TextBox15.Text.Trim());
        prams[15] = new SqlParameter("EUser", TextBox16.Text.Trim());
        prams[16] = new SqlParameter("UserPhone", TextBox17.Text.Trim());
        prams[17] = new SqlParameter("NetConfInfo", TextBox18.Text.Trim());
        prams[18] = new SqlParameter("Remark", TextBox19.Text.Trim());
        prams[19] = new SqlParameter("CPU", TextBox20.Text.Trim());
        prams[20] = new SqlParameter("Mainboard", TextBox21.Text.Trim());
        prams[21] = new SqlParameter("GraphicsCard", TextBox22.Text.Trim());
        prams[22] = new SqlParameter("HDisk", TextBox23.Text.Trim());
        prams[23] = new SqlParameter("NetCard", TextBox24.Text.Trim());
        prams[24] = new SqlParameter("Memory", TextBox25.Text.Trim());
        prams[25] = new SqlParameter("CDROM", TextBox26.Text.Trim());
        prams[26] = new SqlParameter("FloppyDriver", TextBox27.Text.Trim());
        prams[27] = new SqlParameter("Other", TextBox28.Text.Trim());

        if (publicDB.ExcuteNonQuery(addSql, prams) > 0)
        {
            Response.Write("<script>alert('添加成功');window.location.href = window.location.href</script>");
        }
        else
        {
            Response.Write("<script>alert('添加失敗')</script>");
        }
    }

3.2.2 重置、返回操作
原理都是重定向頁面

/// <summary>
    /// 清除頁面數據操作
    /// </summary>
    protected void butClean(object sender, EventArgs e)
    {
        //重定向清除頁面數據
        Response.Write("<script>window.location.href = window.location.href</script>");
    }

    /// <summary>
    /// 返回上級頁面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butBack(object sender, EventArgs e)
    {
        Response.Redirect("equipment.aspx");
    }

3.2.3 修改/更新操作

/// <summary>
    /// 保存/更新操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butSave(object sender, EventArgs e)
    {

        //1 sql
        string updateSQL = "update Assetinfo set Assetinfo.AssetModel=@AssetModel,Assetinfo.EquipNumber=@EquipNumber,Assetinfo.AssetDesc=@AssetDesc,Assetinfo.AssetType=@AssetType,Assetinfo.AssetStat=@AssetStat,Assetinfo.AssetBooker=@AssetBooker,Assetinfo.StockDate=@StockDate,Assetinfo.CheckinDate=@CheckinDate,Assetinfo.YearsOfService=@YearsOfService,Assetinfo.ProduceDate=@ProduceDate,Assetinfo.YearOfUse=@YearOfUse,Assetinfo.SoftwareNo=@SoftwareNo,Assetinfo.RejectDate=@RejectDate,Assetinfo.Unit=@Unit,Assetinfo.EUser=@EUser,Assetinfo.UserPhone=@UserPhone,Assetinfo.NetConfInfo=@NetConfInfo,Assetinfo.Remark=@Remark,Assetinfo.CPU=@CPU,Assetinfo.Mainboard=@Mainboard,Assetinfo.GraphicsCard=@GraphicsCard,Assetinfo.HDisk=@HDisk,Assetinfo.NetCard=@NetCard,Assetinfo.Memory=@Memory,Assetinfo.CDROM=@CDROM,Assetinfo.FloppyDriver=@FloppyDriver,Assetinfo.Other=@Other where AssetNo='" + index + "'";
        //2 獲取頁面全部數據
        SqlParameter[] prams = new SqlParameter[27];
        //prams[0] = new SqlParameter("AssetNo", TextBox1.Text.Trim());
        prams[0] = new SqlParameter("AssetModel", TextBox2.Text.Trim());
        prams[1] = new SqlParameter("EquipNumber", TextBox3.Text.Trim());
        prams[2] = new SqlParameter("AssetDesc", TextBox4.Text.Trim());
        prams[3] = new SqlParameter("AssetType", TextBox5.Text.Trim());
        prams[4] = new SqlParameter("AssetStat", TextBox6.Text.Trim());
        prams[5] = new SqlParameter("AssetBooker", TextBox7.Text.Trim());
        prams[6] = new SqlParameter("StockDate", TextBox8.Text.Trim());
        prams[7] = new SqlParameter("CheckinDate", TextBox9.Text.Trim());
        prams[8] = new SqlParameter("YearsOfService", TextBox10.Text.Trim());
        prams[9] = new SqlParameter("ProduceDate", TextBox11.Text.Trim());
        prams[10] = new SqlParameter("YearOfUse", TextBox12.Text.Trim());
        prams[11] = new SqlParameter("SoftwareNo", TextBox13.Text.Trim());
        prams[12] = new SqlParameter("RejectDate", TextBox14.Text.Trim());
        prams[13] = new SqlParameter("Unit", TextBox15.Text.Trim());
        prams[14] = new SqlParameter("EUser", TextBox16.Text.Trim());
        prams[15] = new SqlParameter("UserPhone", TextBox17.Text.Trim());
        prams[16] = new SqlParameter("NetConfInfo", TextBox18.Text.Trim());
        prams[17] = new SqlParameter("Remark", TextBox19.Text.Trim());
        prams[18] = new SqlParameter("CPU", TextBox20.Text.Trim());
        prams[19] = new SqlParameter("Mainboard", TextBox21.Text.Trim());
        prams[20] = new SqlParameter("GraphicsCard", TextBox22.Text.Trim());
        prams[21] = new SqlParameter("HDisk", TextBox23.Text.Trim());
        prams[22] = new SqlParameter("NetCard", TextBox24.Text.Trim());
        prams[23] = new SqlParameter("Memory", TextBox25.Text.Trim());
        prams[24] = new SqlParameter("CDROM", TextBox26.Text.Trim());
        prams[25] = new SqlParameter("FloppyDriver", TextBox27.Text.Trim());
        prams[26] = new SqlParameter("Other", TextBox28.Text.Trim());
        //3 執行update更新操作
        if (publicDB.ExcuteNonQuery(updateSQL, prams) > 0)
        {
            Response.Write("<script>alert('修改成功');window.location.href = 'equipment.aspx'</script>");
        }
        else
        {
            Response.Write("<script>alert('修改失敗')</script>");
        }
       
    }
  1. 源碼及數據庫鏈接
    鏈接:https://pan.baidu.com/s/1GP8kP0dus6-B4cXTtWVtlg
    提取碼:9249
    複製這段內容後打開百度網盤手機App,操作更方便哦

大佬們點歌讚唄,謝謝啦

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章