Wordy: Today interview, met this superior test, very easy, because of a mistake, incredibly finally did not make out, heart tired
Back home, 1 hours to finish, I have added a secret, I also verify, but God does not give me a chance to start again AH
Very garbage, small white can see, the great God can be lightly sprayed
First, the first question is: The design of a database field
The difference between nvarchar and varchar
Nvarhar: Add a field, automatically fill in the space after the free Varvhar: No space problem arises: when the login judgment, input text box value and the database is the same error, found that there is a space the simplest database table design and then is to add the SQL Help Class library Model code: Model has input validation remember to reference using System.ComponentModel.DataAnnotations in the Model class library;
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.ComponentModel.DataAnnotations;usingSystem.Linq;usingsystem.web;namespacemodel{ Public classUsermodel {[DisplayName ("User name")] [Required (ErrorMessage="must fill in")] Public stringUserName {Get;Set; } [DisplayName ("Password")] [Required (ErrorMessage="must fill in")] [Stringlength ( -, Minimumlength =6, errormessage ="password length must be in {2} and {1} characters")] [DataType (Datatype.password)] Public stringuserpwd {Get;Set; } Public stringConfirmPassword {Get;Set; } Public stringMessage {Get;Set; } }}
Model Code
usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingYL. MySQL;namespacedal{ Public classUserdal {//Register Public BOOLRegst (stringUserName,stringuserpwd) {StringBuilder SQL=NewStringBuilder (); Sql. Append ("INSERT into Users (username,userpwd) VALUES (@UserName, @UserPwd)" ); Sqlparameter[] Sqlparmeter= { NewSqlParameter ("@UserName", SqlDbType.NVarChar, -), NewSqlParameter ("@UserPwd", SqlDbType.NVarChar, -) }; sqlparmeter[0]. Value =UserName; sqlparmeter[1]. Value =userpwd; varresult =NewMysqladoprovide (). ExecuteNonQuery (Mysqlhelper.connectionstringmanager, SQL. ToString (), sqlparmeter) >0; returnresult; } //Login PublicModel.usermodel GetLogin (stringUserName) {StringBuilder strSQL=NewStringBuilder (); Strsql.append ("Select Username,userpwd" ); Strsql.append ("From Users"); Strsql.append ("where [email protected]"); sqlparameter[] Parameters= { NewSqlParameter ("@UserName", SqlDbType.NVarChar, -) }; parameters[0]. Value =UserName; vardata =NewMysqladoprovide (). ExecuteReader (dr = Dr. Getentity<model.usermodel>(), Mysqlhelper.connectionstringmanager, strsql.tostring (), parameters). FirstOrDefault (); returndata; } }}
DAL code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacebll{ Public classUSERBLL {//constructor Function PrivateDAL. Userdal dal; Publicuserbll () {dal=NewDAL. Userdal (); } //Register Public BOOLRegst (stringUserName,stringuserpwd) { returndal.regst (UserName, userpwd); } PublicModel.usermodel GetLogin (stringUserName) { returndal. GetLogin (UserName); } }}
BLL Code
Mvcapplication8-homecontroller Code Controller Code
@model model.usermodel@{viewbag.title="Index";}@using (Html.BeginForm ("Login","Home", FormMethod.Post)) { <div>@Html. textboxfor (M= M.username,New{@class ="Form-control", @placeholder ="User name"}) @Html. validationmessagefor (M=m.username) @Html. Textboxfor (M= M.userpwd,New{@class ="Form-control", @placeholder ="Password"}) @Html. validationmessagefor (M=m.userpwd)<button type="Submit"> Login </button>@Html. validationmessagefor (M=m.message)<a href="@Url. Action ("Regist")"> Register a new account </a> </div> }</div>
Login Interface Code
@model model.usermodel@{viewbag.title="Index";}@using (Html.BeginForm ("ZC","Home", FormMethod.Post)) { <div>@Html. textboxfor (M= M.username,New{@placeholder ="User name"}) @Html. validationmessagefor (M=m.username) @Html. Textboxfor (M= M.userpwd,New{@placeholder ="Password"}) @Html. validationmessagefor (M=m.userpwd) @Html. Textboxfor (M= M.confirmpassword,New{@placeholder ="Confirm Password"}) @Html. validationmessagefor (M=M.confirmpassword) @Html. Validationmessagefor (M=m.message)<button type="Submit"> Registration </button> </div> }</div>
Registration Interface Code
Login + register (three layer +sql statement)