最近期末比较忙,没时间更新博客,下面我将对我自己做的一个ASP.NET项目(设备管理系统)进行总结,页面模板用的是母版,这样大大减少了代码量。最后在文章底部附上项目源码及数据库。
先看效果吧
1.1 登录页
1.2 欢迎页
1.3 主页/列表页
1.4 查询效果(全字段查询)
1.5 详情页/新建、修改
登录示例demo
为让大家看的更加清楚,我将连接连接数据库的操作直接写在登录示例里了。并且考虑数据安全性,我将密码进行了哈希加密。
///
/// 登录
///
///
///
[Obsolete]
protected void Button1_Click(object sender, EventArgs e)
{
//【1】 判断账号密码是否为空
if (TextBox1.Text.Trim() =="" || TextBox2.Text.Trim() == "")
{
//提示账号密码不能为空
//Response.Write("");
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();
}
}
///
/// 封装了绑定数据操作
///
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(全字段)查询的核心代码
///
/// 查询操作
///
///
///
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" };
}
///
/// 查询操作
///
///
///
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();
}
///
/// 翻页操作
///
///
///
protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
///
/// 删除操作
///
///
///
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("");
}
else
{
Response.Write("");
}
}
///
/// 封装了绑定数据操作
///
public void BindDates()
{
//实例化类
publicDB pb = new publicDB();
//查询sql语句
string strsql = "select * from Assetinfo";
//调用BindDate函数
object set = pb.BindDate(strsql);
//绑定数据源
GridView1.DataSource = set;
GridView1.DataBind();
}
///
/// 封装了传递sql语句调用ExceSQL执行sql语句的方法
///
/// 要执行的sql语句
/// 返回sql语句执行的状态,即成功与否
public bool ExceSQLs(string sqlStr)
{
//实例化类
publicDB pb = new publicDB();
//调用ExceSQL执行sql语句返回bool值(即sql执行成功与否)
bool sqtState = pb.ExceSQL(sqlStr);
return sqtState;
}
///
/// 修改操作
///
///
///
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string id = GridView1.DataKeys[e.NewEditIndex]["AssetNo"].ToString();
Response.Redirect("equipmentUpdate.aspx?AssetNo=" + id);
}
///
/// 点击新建跳转到编辑详情页
///
///
///
protected void toAdd(object sender, EventArgs e)
{
Response.Redirect("equipmentAdd.aspx");
}
///
/// 刷新操作
///
///
///
protected void refresh(object sender, EventArgs e)
{
Response.Write("");
}
}
3.2 刚刚前面说到了跳转详情,下面介绍详情页。
因为这个项目的字段比较多(28个),因此直接在共一个页面进行修应该添加操作不美观,因此我特意添加了一个详情页用来完成添加/修改数据的操作。
主要功能就包括,保存、 清空(重置)、新建、返回等功能
代码如下:
3.2.1 添加操作
Tip: 这里面用到了一个外部的公共类publicDB,这个类是我自己写的,封装了对数据/数据库的基本操作,如有连接数据、执行增删改查的基本操作等。
///
/// 保存/更新操作
///
///
///
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("");
}
else
{
Response.Write("");
}
}
3.2.2 重置、返回操作
原理都是重定向页面
///
/// 清除页面数据操作
///
protected void butClean(object sender, EventArgs e)
{
//重定向清除页面数据
Response.Write("");
}
///
/// 返回上级页面
///
///
///
protected void butBack(object sender, EventArgs e)
{
Response.Redirect("equipment.aspx");
}
3.2.3 修改/更新操作
///
/// 保存/更新操作
///
///
///
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("");
}
else
{
Response.Write("");
}
}
大佬们点歌赞呗,谢谢啦