写一个基于ASP.CODE的酒店入住程序
时间: 2023-12-19 20:05:16 浏览: 66
好的,以下是一个基于ASP.NET的酒店入住程序的示例:
1. 创建数据库表格
创建一个名为“Bookings”的表格,其中包含以下列:
- BookingID (int, primary key, identity)
- GuestName (nvarchar(50))
- CheckInDate (date)
- CheckOutDate (date)
- RoomType (nvarchar(50))
- RoomNumber (nvarchar(50))
2. 创建ASP.NET页面
创建一个名为“BookRoom.aspx”的ASP.NET页面,其中包含以下控件:
- TextBox控件,用于输入客人姓名
- Calendar控件,用于选择入住日期
- Calendar控件,用于选择退房日期
- DropDownList控件,用于选择房间类型
- Button控件,用于提交预订信息
3. 添加代码
在页面的CodeBehind中添加以下代码:
```csharp
protected void btnBookRoom_Click(object sender, EventArgs e)
{
// 获取输入的预订信息
string guestName = txtGuestName.Text;
DateTime checkInDate = calCheckInDate.SelectedDate;
DateTime checkOutDate = calCheckOutDate.SelectedDate;
string roomType = ddlRoomType.SelectedValue;
// 验证输入的日期是否合法
if (checkOutDate <= checkInDate)
{
Response.Write("退房日期必须晚于入住日期");
return;
}
// 查询可用的房间号
string roomNumber = GetAvailableRoomNumber(roomType, checkInDate, checkOutDate);
// 如果找不到可用的房间号,则提示客人无法预订
if (string.IsNullOrEmpty(roomNumber))
{
Response.Write("目前没有可用的房间,请选择其他日期或房间类型");
return;
}
// 将预订信息插入到数据库中
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand("INSERT INTO Bookings (GuestName, CheckInDate, CheckOutDate, RoomType, RoomNumber) VALUES (@GuestName, @CheckInDate, @CheckOutDate, @RoomType, @RoomNumber)", connection);
command.Parameters.AddWithValue("@GuestName", guestName);
command.Parameters.AddWithValue("@CheckInDate", checkInDate);
command.Parameters.AddWithValue("@CheckOutDate", checkOutDate);
command.Parameters.AddWithValue("@RoomType", roomType);
command.Parameters.AddWithValue("@RoomNumber", roomNumber);
connection.Open();
command.ExecuteNonQuery();
}
// 显示预订成功的消息,并重置输入控件
Response.Write("预订成功!您的房间号是 " + roomNumber);
txtGuestName.Text = "";
calCheckInDate.SelectedDate = DateTime.Today;
calCheckOutDate.SelectedDate = DateTime.Today.AddDays(1);
ddlRoomType.SelectedIndex = 0;
}
private string GetAvailableRoomNumber(string roomType, DateTime checkInDate, DateTime checkOutDate)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand("SELECT TOP 1 RoomNumber FROM Rooms WHERE RoomType = @RoomType AND RoomNumber NOT IN (SELECT RoomNumber FROM Bookings WHERE CheckOutDate > @CheckInDate AND CheckInDate < @CheckOutDate)", connection);
command.Parameters.AddWithValue("@RoomType", roomType);
command.Parameters.AddWithValue("@CheckInDate", checkInDate);
command.Parameters.AddWithValue("@CheckOutDate", checkOutDate);
connection.Open();
object result = command.ExecuteScalar();
if (result != null)
{
return result.ToString();
}
else
{
return "";
}
}
}
```
4. 创建数据库连接字符串
在Web.config文件中添加以下数据库连接字符串:
```xml
<configuration>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyHotel;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
```
其中,Data Source是SQL Server的实例名称,Initial Catalog是数据库名称,Integrated Security=True表示使用Windows身份验证连接数据库。
以上就是一个基于ASP.NET的酒店入住程序的示例,可以根据自己的需求进行修改和扩展。
阅读全文