我的第一个ASP.NET的程序的感受

本文分享了作者从Java转向.NET平台的心得体会,并详细介绍了使用C#实现的一个用户登录程序,包括业务逻辑层、数据访问层、模型层及前台页面层的设计与实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于公司人手比较少,而且项目比较多,使用java就我一个人,所以最后综合考虑转.net半年时间,我想正好让我学习一下.net里面好的长处,不知道是祸是福,我想某一方面我的java开发可能会受点影响,但应该也让我看不同语言的特点。

今天作的第一个小的程序就是一个用户登陆程序,

业务逻辑层代码:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Login.DAL; namespace Login.BLL { public static partial class Tbl_UserInfoManager { public static int Login(string userId, string password) { return Tbl_UserInfoService.Login(userId, password); } } }

数据访问层:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using Login.Models; namespace Login.DAL { public static partial class Tbl_UserInfoService { public static int Login(string userId, string password) { int resultId = 0; string sql = "select * from tbl_user where userId='" + userId + "' and password='" + password + "'"; Tbl_UserInfo userinfo=userLogin(userId); if (userinfo.userId == userId && userinfo.password == password) { resultId = 1; } else { resultId = 0; } return resultId; } public static Tbl_UserInfo userLogin(string userName) { string sql = "select * from tbl_user where userId=@userId"; SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@userId",userName)); if (reader.Read()) { Tbl_UserInfo userinfo = new Tbl_UserInfo(); userinfo.userId = (string )reader["userId"]; userinfo.password = (string)reader["password"]; return userinfo; } else { reader.Close(); return null; } } } }

模型层:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Login.Models { public class Tbl_UserInfo { public string userId; public string password; public Tbl_UserInfo() { } public string UserId { get { return this.userId; } set { this.userId = value; } } public string Password { get { return this.password; } set { this.password = value; } } } }

前台页面层:

using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Login.Models; using Login.BLL; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { userId.Text= ""; password.Text = ""; } } protected void btnSubmit_Click(object sender, EventArgs e) { if (userId.Text != "" && password.Text != "") { //try //{ // //Console.WriteLine("yyyyyyy1"); // Tbl_UserInfo userInfo = new Tbl_UserInfo(); // int resultNum = Tbl_UserInfoManager.Login(userId.Text.ToString(), password.Text.ToString()); // if (resultNum !=0) // { // Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!-- alert('恭喜您,登陆成功') // --></mce:script>"); // } // else // { // Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!-- alert('填写的信息错误!'); // --></mce:script>"); // } //} //catch(Exception) //{ // Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!-- alert('填写的信息失败!'); // --></mce:script>"); //} Tbl_UserInfo userInfo = new Tbl_UserInfo(); int resultNum = Tbl_UserInfoManager.Login(userId.Text.ToString(), password.Text.ToString()); if (resultNum !=0) { Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!-- alert('恭喜您,登陆成功') // --></mce:script>"); } else { Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!-- alert('填写的信息错误!'); // --></mce:script>"); } } else { Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!-- alert('请填写完整信息!'); // --></mce:script>"); } } protected void btnReset_Click(object sender, EventArgs e) { userId.Text = ""; password.Text = ""; } }

感觉.net前台的页面显示层方面的效率真的非常优越,我想以后可以自己封装试试

对于后台处理我认为差不多

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值