訂閱
糾錯(cuò)
加入自媒體

Web應(yīng)用開發(fā):ASP.NET 大學(xué)場(chǎng)地預(yù)約借用系統(tǒng)

5.2 后臺(tái)

登錄頁(yè)面

后臺(tái)代碼進(jìn)行校驗(yàn),需要先將密碼轉(zhuǎn)成MD5密文,然后實(shí)行字符串匹配:

string username = Request.Params["inputEmail"].ToString();
string password = MD5Helper.ToMD5(Request.Params["inputPassword"].ToString());
if (DBHelper.ExecuteQuery("select * from WebUser where username='" + username + "' and password='" + password + "'").Tables[0].Rows.Count > 0)

   //放一個(gè)Cookie來(lái)指示是哪名用戶登陸了
   HttpCookie cookie = new HttpCookie("login_name", username);
   Response.Cookies.Add(cookie);
   Response.Redirect("indextem.a(chǎn)spx");

else

   Response.Write("<script language=j(luò)avascript>alert('用戶名或密碼錯(cuò)誤');</script>");

注冊(cè)頁(yè)面的前端類似,后端代碼以數(shù)據(jù)庫(kù)插入為主,需要判斷用戶名是否重復(fù),如下:

var name = Request["regName"];
if (!string.IsNullOrEmpty(name))

   if (DBHelper.ExecuteQuery("select * from WebUser where username='" + name + "'").Tables[0].Rows.Count > 0)
   {
       //檢測(cè)該用戶名是否注冊(cè)
       Response.Write("<script language=j(luò)avascript>alert('注冊(cè)失敗,用戶名已被使用。В;</script>");
   }
   else
   {
       var passwd = Request["regPassword"];
       passwd = MD5Helper.ToMD5(passwd.ToString());
       var telephone = Request["regTelephone"];
       var sql = "INSERT INTO WebUser(username,password,telephone) VALUES ('{0}','{1}','{2}')";
       sql = string.Format(sql, name, passwd, telephone);
       if (SqlHelper.ExecuteSql(sql) > 0)
       {
           var str = "注冊(cè)成功,您的用戶名:" + name + " ,現(xiàn)在去登錄試試吧~";
           Response.Write("<script language=j(luò)avascript>alert('" + str + "');</script>");
       }
       else
       {
           Response.Write("<script language=j(luò)avascript>alert('注冊(cè)失敗,數(shù)據(jù)庫(kù)出錯(cuò)!');</script>");
       }
   }

預(yù)約頁(yè)面

預(yù)定的后臺(tái)處理代碼,后臺(tái)需要做預(yù)定沖突的檢測(cè):

string[] bookTime = context.Request["bookTime"].Split('-');
string bookSt = bookTime[0].Trim();
string bookEt = bookTime[1].Trim();
string bookDate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
string roomNumber = context.Request["roomNo"];
//預(yù)定時(shí)間區(qū)間判斷
var bookInfoSql = "select BookSt, BookSt from BookInfo where " +
   "BookDate='{0}' And RoomNumber='{1}'";
bookInfoSql = string.Format(bookInfoSql, bookDate, roomNumber);
DataTable dtBookInfo = SqlHelper.getDataTable(bookInfoSql);
Boolean notOverlap = true;
for (int i = 0; i < dtBookInfo.Rows.Count; i++)

   //大于已預(yù)約右邊,小于已預(yù)約左邊
   notOverlap &= ( (string.Compare(bookSt, dtBookInfo.Rows[i][1].ToString().Trim(), true) > 0) ||
                   (string.Compare(bookEt, dtBookInfo.Rows[i][0].ToString().Trim(), true) < 0) );

if (!notOverlap)

   context.Response.Write("該時(shí)間段已經(jīng)有別人預(yù)約啦,請(qǐng)重新選擇。ⅲ

else

   string customerName = context.Request.Cookies["login_name"].Value;
   string myRemark = context.Request["myRemark"];
   if (string.IsNullOrEmpty(myRemark))
   {
       myRemark = "無(wú)";
   }
   DateTime dt1 = DateTime.Parse(bookDate + " " + bookSt);
   DateTime dt2 = DateTime.Parse(bookDate + " " + bookEt);
   TimeSpan ts = dt2.Subtract(dt1);
   double bookDurationHours = Math.Round(ts.TotalHours, 2);
   DataTable dtLastID = SqlHelper.getDataTable("select top 1 ID from BookInfo order by ID DESC");
   string insIdStr = dtLastID.Rows[0][0].ToString();
   int insId = int.Parse(insIdStr) + 1;
   //插入到數(shù)據(jù)庫(kù)中去
   var bookSql = "INSERT INTO BookInfo(ID, CustomerName,MyRemark,BookDate,BookSt,BookDuration,RoomNumber,BookEt) " +
       "VALUES ({0},'{1}','{2}','{3}','{4}','{5}','{6}','{7}')";
   bookSql = string.Format(bookSql, insId, customerName, myRemark, bookDate, bookSt, bookDurationHours,
       roomNumber, bookEt);
   if (SqlHelper.ExecuteSql(bookSql) > 0)
   {
       context.Response.Write("預(yù)定成功。ⅲ;
   }
   else
   {
       context.Response.Write("后臺(tái)數(shù)據(jù)插入出錯(cuò)。ⅲ;
   }

獲取預(yù)訂時(shí)間段后臺(tái)處理代碼:

string roomNum = context.Request["roomNo"];
var sqlBookSp = "select BookSt, BookEt from BookInfo " +
   "where BookDate = '" + DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + "' and RoomNumber = '{0}' order by BookSt";
sqlBookSp = string.Format(sqlBookSp, roomNum);
DataTable dtTimeSp = SqlHelper.getDataTable(sqlBookSp);
string sJson2 = JsonConvert.SerializeObject(dtTimeSp);
context.Response.Write(sJson2);
break;

取消預(yù)約的代碼:

//刪除預(yù)訂的數(shù)據(jù)
string[] delListStr = context.Request["cancel"].Split(',');
int totalCancel = 0;
foreach(var delRoom in delListStr)

 var delSql = "delete from BookInfo where ID = " + delRoom;
 if (SqlHelper.ExecuteSql(delSql) > 0)
 {
     
     totalCancel++;
 }

context.Response.Write("取消預(yù)訂完成,共取消 " + totalCancel.ToString() + " 間教室。ⅲ;

6、運(yùn)行效果圖

登錄

注冊(cè):

預(yù)訂(可以手動(dòng)刷新教室信息):

聲明: 本文由入駐維科號(hào)的作者撰寫,觀點(diǎn)僅代表作者本人,不代表OFweek立場(chǎng)。如有侵權(quán)或其他問(wèn)題,請(qǐng)聯(lián)系舉報(bào)。

發(fā)表評(píng)論

0條評(píng)論,0人參與

請(qǐng)輸入評(píng)論內(nèi)容...

請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字

您提交的評(píng)論過(guò)于頻繁,請(qǐng)輸入驗(yàn)證碼繼續(xù)

  • 看不清,點(diǎn)擊換一張  刷新

暫無(wú)評(píng)論

暫無(wú)評(píng)論

人工智能 獵頭職位 更多
掃碼關(guān)注公眾號(hào)
OFweek人工智能網(wǎng)
獲取更多精彩內(nèi)容
文章糾錯(cuò)
x
*文字標(biāo)題:
*糾錯(cuò)內(nèi)容:
聯(lián)系郵箱:
*驗(yàn) 證 碼:

粵公網(wǎng)安備 44030502002758號(hào)