由於公司人手比較少,而且項目比較多,使用java就我一個人,所以最後綜合考慮轉.net半年時間,我想正好讓我學習一下.net裡面好的長處,不知道是禍是福,我想某一方面我的java開發可能會受點影響,但應該也讓我看不同語言的特點。
今天作的第一個小的程式就是一個使用者登陸程式,
商務邏輯層代碼:
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;</p><p>using Login.DAL;</p><p>namespace Login.BLL<br />{<br /> public static partial class Tbl_UserInfoManager<br /> {<br /> public static int Login(string userId, string password)<br /> {<br /> return Tbl_UserInfoService.Login(userId, password);<br /> }<br /> }<br />}<br />
資料訪問層:
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;<br />using System.Data;<br />using System.Data.SqlClient;</p><p>using Login.Models;</p><p>namespace Login.DAL<br />{<br /> public static partial class Tbl_UserInfoService<br /> {<br /> public static int Login(string userId, string password)<br /> {<br /> int resultId = 0;</p><p> string sql = "select * from tbl_user where userId='" + userId + "' and password='" + password + "'";</p><p> Tbl_UserInfo userinfo=userLogin(userId);<br /> if (userinfo.userId == userId && userinfo.password == password)<br /> {<br /> resultId = 1;<br /> }<br /> else<br /> {<br /> resultId = 0;<br /> }<br /> return resultId;</p><p> }</p><p> public static Tbl_UserInfo userLogin(string userName)<br /> {</p><p> string sql = "select * from tbl_user where userId=@userId";<br /> SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@userId",userName));<br /> if (reader.Read())<br /> {</p><p> Tbl_UserInfo userinfo = new Tbl_UserInfo();</p><p> userinfo.userId = (string )reader["userId"];<br /> userinfo.password = (string)reader["password"];</p><p> return userinfo;<br /> }<br /> else<br /> {<br /> reader.Close();<br /> return null;<br /> }<br /> }<br /> }<br />}<br />
模型層:
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;</p><p>namespace Login.Models<br />{<br /> public class Tbl_UserInfo<br /> {<br /> public string userId;</p><p> public string password;</p><p> public Tbl_UserInfo()<br /> {<br /> }</p><p> public string UserId<br /> {<br /> get<br /> {<br /> return this.userId;<br /> }<br /> set<br /> {<br /> this.userId = value;<br /> }<br /> }</p><p> public string Password<br /> {<br /> get<br /> {<br /> return this.password;<br /> }<br /> set<br /> {<br /> this.password = value;<br /> }<br /> }<br /> }<br />}<br />
前台頁面層:
using System;<br />using System.Collections;<br />using System.Configuration;<br />using System.Data;<br />using System.Linq;<br />using System.Web;<br />using System.Web.Security;<br />using System.Web.UI;<br />using System.Web.UI.HtmlControls;<br />using System.Web.UI.WebControls;<br />using System.Web.UI.WebControls.WebParts;</p><p>using Login.Models;<br />using Login.BLL;</p><p>public partial class _Default : System.Web.UI.Page<br />{<br /> protected void Page_Load(object sender, EventArgs e)<br /> {<br /> if (!IsPostBack)<br /> {<br /> userId.Text= "";<br /> password.Text = "";</p><p> }<br /> }</p><p> protected void btnSubmit_Click(object sender, EventArgs e)<br /> {<br /> if (userId.Text != "" && password.Text != "")<br /> {<br /> //try<br /> //{<br /> // //Console.WriteLine("yyyyyyy1");<br /> // Tbl_UserInfo userInfo = new Tbl_UserInfo();</p><p> // int resultNum = Tbl_UserInfoManager.Login(userId.Text.ToString(), password.Text.ToString());</p><p> // if (resultNum !=0)<br /> // {<br /> // Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!--<br />alert('恭喜您,登陸成功')<br />// --></mce:script>");<br /> // }<br /> // else<br /> // {<br /> // Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!--<br />alert('填寫的資訊錯誤!');<br />// --></mce:script>");<br /> // }</p><p> //}<br /> //catch(Exception)<br /> //{<br /> // Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!--<br />alert('填寫的資訊失敗!');<br />// --></mce:script>");<br /> //}</p><p> Tbl_UserInfo userInfo = new Tbl_UserInfo();</p><p> int resultNum = Tbl_UserInfoManager.Login(userId.Text.ToString(), password.Text.ToString());</p><p> if (resultNum !=0)<br /> {<br /> Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!--<br />alert('恭喜您,登陸成功')<br />// --></mce:script>");<br /> }<br /> else<br /> {<br /> Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!--<br />alert('填寫的資訊錯誤!');<br />// --></mce:script>");<br /> }</p><p> }<br /> else<br /> {<br /> Page.ClientScript.RegisterStartupScript(GetType(), "", "<mce:script type="text/javascript"><!--<br />alert('請填寫完整資訊!');<br />// --></mce:script>");<br /> }<br /> }</p><p> protected void btnReset_Click(object sender, EventArgs e)<br /> {<br /> userId.Text = "";<br /> password.Text = "";<br /> }</p><p>}<br />
感覺.net前台的頁面顯示層方面的效率真的非常優越,我想以後可以自己封裝試試
對於幕後處理我認為差不多