客房预订业务的特点是顾客流动性大,其用户注册方式更为灵活,预订业务除了常用的验证外,还要防止在预订满的情况下继续预订客房。本实例将为读者介绍如何实现客房预订业务。
效果说明 图 96-1 所示是预订业务的基本界面,用户可以直接登录或注册并预订客房。
( 3 )建立数据库链接“ ADconn ”,链接数据库“ C:\ Inetpub\wwwroot\adweb \data\hotel.mdb ”,方法可参考实例 75 。
步骤二 设计基本页面
( 1 )“ index.aspx ”页面结构。打开“ index.aspx ”,该页面是素材直接提供的,初始打开时如图 96-6 所示。图中①、②所示是图像而不是图像按钮,圆角方框①所示的图像其作用是添加“ OnClick ”动作以显示“ Layer1 ”层,该层中添加了一个日历控件,其 ID 为“ MyCalendar1 ”;圆角方框②所示图像的作用是添加“ OnClick ”动作以显示“ Layer2 ”层,该层中添加了一个日历控件,其 ID 为“ MyCalendar2 ”。
提示:“ index.aspx ”初始页面中其他控件的设置情况如表 96-1 所示。
表 96-1 “ index.aspx ”初始页面中其他控件的设置情况
控件对应文本 |
控件类型 |
控件 ID |
入住时间 |
asp :文本框 |
StartTime |
离店时间 |
asp :文本框 |
LastTime |
预订数量 |
asp :文本框 |
OrderNum |
会员 ID |
asp :文本框 |
UserID1 |
会员密码 |
asp :文本框 |
UserPass1 |
注册 ID |
asp :文本框 |
UserID |
真实姓名 |
asp :文本框 |
UseName |
登录密码 |
asp :文本框 |
UserPass |
重复密码 |
asp :文本框 |
UserPass2 |
电子邮件 |
asp :文本框 |
UserEmail |
联系电话 |
asp :文本框 |
UserTel |
【现在预订】按钮 |
asp :按钮 |
Orderbt |
【全部重置】按钮 |
表单按钮 |
Submit (其动作为重置表单) |
( 2 )添加数据集【 RoomData 】。在【服务器行为】面板中添加数据集【 RoomData 】,其在【数据集】对话框中的设置如图 96-7 所示。
( 3 )修改 SQL 命令。在【数据集】对话框中单击【高级】按钮,切换到【数据集】高级对话框,将 SQL 命令由 SELECT Pice,RoomID, RoomNum,RoomType FROM tbRoom 改为 SELECT Pice,RoomID, RoomNum, trim(RoomType)+chr(32)+cstr(Pice) As RoomType FROM tbRoom ,如图 96-8 所示。
( 4 )添加数据列表。单击 ASP.NET 快捷菜单中的【 asp :单选按钮列表】,操作如图 96-9 所示。
( 5 )为页面添加“ System.Data ”、“ System.Data.OleDb ”、“ System.Globalization ”和“ System.Threading ” 4 个命名空间,如图 96-10 中圆角方框所示。 ( 6 )定义页面载入过程“ Page_Load ”,该过程的代码如下所述。 Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs) (读者可打开【光盘】|【源文件】|【实例 96 】|【 96.1.txt 】文件,直接复制) 程序说明: 该 “ Page_Load ”过程是在第一次载入页面时,为单选按钮列表“ RoomRadio ”添加列表项,添加列表项的标签值等于“ RoomData ”数据集下的字段“ RoomType ”,而选取值等于字段“ RoomID ”。 提示:也许有人会问为什么不直接用单选按钮列表的数据绑定呢?这由于通过 Dreamweaver 进行数据绑定,在程序中经常会出现无法获取单选按钮列表选取值的错误。 ( 7 )定义“ MyCalendar1_SelectionChanged ”过程,用于响应日历控件“ MyCalendar1 ”的“ SelectionChanged ”事件,该过程的代码如下所述。 Protected Sub MyCalendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)Handles MyCalendar1.SelectionChanged (读者可打开【光盘】|【源文件】|【实例 96 】|【 96.2.txt 】文件,直接复制) ( 8 )定义“ MyCalendar2_SelectionChanged ”过程,用于响应日历控件“ MyCalendar2 ”的“ SelectionChanged ”事件,该过程的代码如下所述。 Protected Sub MyCalendar2_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)Handles MyCalendar2.SelectionChanged (读者可打开【光盘】|【源文件】|【实例 96 】|【 96.3.txt 】文件,直接复制) |
步骤三 实现客户预订业务
( 1 )实现客户预订业务的程序流程如图 96-11 所示。
( 2 )为页面添加 4 个全局变量,如图 96-12 中圆角方框所示。
( 3 )定义“ Usercheck ”函数。该函数用于检测用户是否登录正确,登录不正确时是否注册成功,如果用户登录正确或者注册成功,则该函数返回“ True ”,否则将返回“ False ”,该函数的代码如下所述。
以下是引用片段: Function Usercheck() As Boolean Dim Hotelada1, Hotelada2 As OleDbDataAdapter Dim HotelDataSet1, HotelDataSet2 As System.Data.DataSet Dim Hoteldt1, Hoteldt2 As DataTable Dim tbRow As DataRow Dim yy As OleDbCommandBuilder Hotelstring = "Select * From tbUser Where UserId='" & UserID1.Text & "' And UserPass='" & UserPass1.Text & "'" Hotelada1 = New OleDbDataAdapter(Hotelstring, conn) HotelDataSet1 = New System.Data.DataSet Hotelada1.Fill(HotelDataSet1, "tbUser") Hoteldt1 = HotelDataSet1.Tables("tbUser") If Hoteldt1.Rows.Count > 0 Then strName = Hoteldt1.Rows(0)("UserId") Else If (Trim(UserID.Text) <> Nothing) And (Trim(UserPass.Text) <> Nothing) _ And (Trim(UserName.Text) <> Nothing) And (Trim(UserEmail.Text) <> Nothing) _ And (Trim(UserTel.Text) <> Nothing) Then Hotelstring = "Select * from tbUser" Hotelada2 = New OleDbDataAdapter(Hotelstring, conn) yy = New OleDbCommandBuilder(Hotelada2) HotelDataSet2 = New System.Data.DataSet Hotelada2.Fill(HotelDataSet2, "tbUser") Hoteldt2 = HotelDataSet2.Tables("tbUser") tbRow = Hoteldt2.NewRow tbRow("UserId") = UserID.Text tbRow("UserPass") = UserPass.Text tbRow("UserName") = UserName.Text tbRow("UserEmail") = UserEmail.Text tbRow("UserTel") = UserTel.Text Hoteldt2.Rows.Add(tbRow) Hotelada2.Update(HotelDataSet2, "tbUser") strName = UserID.Text Else Usercheck = False Exit Function End If End If Usercheck = True End Function |
以下是引用片段: Function Roomcheck() As Boolean Dim Hotelada1, Hotelada2 As OleDbDataAdapter Dim HotelDataSet1, HotelDataSet2 As System.Data.DataSet Dim Hoteldt1, Hoteldt2 As DataTable Dim i, RoomI As Integer If RoomRadio.SelectedItem.Value <> Nothing Then Hotelstring = "Select * From UserOrder Where RoomID=" & RoomRadio.SelectedItem.Value Hotelada1 = New OleDbDataAdapter(Hotelstring, conn) HotelDataSet1 = New System.Data.DataSet Hotelada1.Fill(HotelDataSet1, "UserOrder") Hoteldt1 = HotelDataSet1.Tables("UserOrder") Dim Date1, Date2 As Date Date1 = StartTime.Text Date2 = LastTime.Text For i = 0 To Hoteldt1.Rows.Count - 1 Dim roomdate1, roomdate2 As Date roomdate1 = Hoteldt1.Rows(i)("StartTime") roomdate2 = Hoteldt1.Rows(i)("LastTime") If (System.DateTime.Compare(Date1, roomdate1) And System.DateTime.Compare(roomdate2, Date1)) _ Or (System.DateTime.Compare(Date2, roomdate1) And System.DateTime.Compare(roomdate2, Date2)) _ Or (System.DateTime.Compare(roomdate1, Date1) And System.DateTime.Compare(Date2, roomdate2)) Then RoomI += 1 End If Next Hotelstring = "select RoomNum From tbRoom where RoomID=" & RoomRadio.SelectedItem.Value Hotelada2 = New OleDbDataAdapter(Hotelstring, conn) HotelDataSet2 = New System.Data.DataSet Hotelada1.Fill(HotelDataSet2, "tbRoom") Hoteldt2 = HotelDataSet2.Tables("tbRoom") If RoomI +CInt(OrderNum.Text) > Hoteldt2.Rows(0)("RoomNum") Then Roomcheck = False Exit Function End If Else Roomcheck = False Exit Function End If Roomcheck = True End Function |
以下是引用片段: Sub Orderinser() Dim Hotelada2 As OleDbDataAdapter Dim HotelDataSet2 As System.Data.DataSet Dim Hoteldt2 As DataTable Dim tbRow As DataRow Dim yy As OleDbCommandBuilder Hotelstring = "Select * From tbOrder" Hotelada2 = New OleDbDataAdapter(Hotelstring, conn) HotelDataSet2 = New System.Data.DataSet yy = New OleDbCommandBuilder(Hotelada2) Hotelada2.Fill(HotelDataSet2, "tbOrder") Hoteldt2 = HotelDataSet2.Tables("tbOrder") tbRow = Hoteldt2.NewRow tbRow("UserID") = strName tbRow("RoomID") = CInt(RoomRadio.SelectedItem.Value) tbRow("OrderNum") = CInt(OrderNum.Text) tbRow("StartTime") = StartTime.Text tbRow("LastTime") = LastTime.Text HotelDataSet2.Tables("tbOrder").Rows.Add(tbRow) Hotelada2.Update(HotelDataSet2, "tbOrder") End Sub |
以下是引用片段: Sub ErrInfo(ByVal Errmess As String) Dim strScript As String strScript = "<script language=javascript>" strScript += "alert('" + Errmess + "');" strScript += "<" strScript += "/" strScript += "script>" RegisterClientScriptBlock("showSaveMessage", strScript) End Sub |
以下是引用片段: Protected Sub Orderbt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim Errstr As String HotelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("data/hotel.mdb") conn = New OleDbConnection(HotelConn) If Usercheck() = False Then Errstr = " 请正确登录或正确输入您的注册信息! " ErrInfo(Errstr) Exit Sub End If If Roomcheck() = False Then Errstr = " 您预订客房在您预订时间段中已预订满了! " ErrInfo(Errstr) Exit Sub End If Orderinser() End Sub |