用csharp 写一个完整的自习室作为预选系统包含数据库
时间: 2023-11-30 08:02:37 浏览: 120
好的,我可以为您提供一个基于C#和SQL Server的自习室预选系统的简单实现。
首先,我们需要创建一个数据库来存储自习室的信息和预选信息。我们可以使用SQL Server Management Studio来创建一个名为“StudyRoom”的数据库,并创建两个表格:一个用于存储自习室的信息,另一个用于存储预选信息。下面是两个表格的创建语句:
```sql
CREATE TABLE StudyRooms (
RoomID INT PRIMARY KEY IDENTITY,
RoomName VARCHAR(50) NOT NULL,
RoomCapacity INT NOT NULL,
RoomLocation VARCHAR(50) NOT NULL
);
CREATE TABLE RoomReservations (
ReservationID INT PRIMARY KEY IDENTITY,
RoomID INT NOT NULL,
ReservationDate DATE NOT NULL,
StartTime TIME NOT NULL,
EndTime TIME NOT NULL,
StudentName VARCHAR(50) NOT NULL,
CONSTRAINT FK_RoomReservations_StudyRooms FOREIGN KEY (RoomID) REFERENCES StudyRooms(RoomID)
);
```
接下来,我们可以使用Visual Studio来创建一个C#控制台应用程序,并将其命名为“StudyRoomReservationSystem”。
在程序中,我们需要使用ADO.NET来连接到SQL Server数据库并执行SQL查询语句。我们可以创建一个名为“DatabaseHelper”的类来实现这个功能。下面是这个类的代码:
```csharp
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace StudyRoomReservationSystem
{
public static class DatabaseHelper
{
private static string connectionString = ConfigurationManager.ConnectionStrings["StudyRoom"].ConnectionString;
public static DataTable ExecuteQuery(string query)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
return dataTable;
}
}
}
}
public static void ExecuteNonQuery(string query)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
command.ExecuteNonQuery();
}
}
}
}
}
```
接下来,我们可以创建一个名为“StudyRoom”的类来表示自习室,并实现一些基本的功能,如获取自习室列表、添加自习室和查询可用时间段等。下面是这个类的代码:
```csharp
using System;
using System.Collections.Generic;
using System.Data;
namespace StudyRoomReservationSystem
{
public class StudyRoom
{
public int RoomID { get; set; }
public string RoomName { get; set; }
public int RoomCapacity { get; set; }
public string RoomLocation { get; set; }
public static List<StudyRoom> GetStudyRooms()
{
List<StudyRoom> studyRooms = new List<StudyRoom>();
DataTable dataTable = DatabaseHelper.ExecuteQuery("SELECT * FROM StudyRooms");
foreach (DataRow row in dataTable.Rows)
{
StudyRoom studyRoom = new StudyRoom();
studyRoom.RoomID = Convert.ToInt32(row["RoomID"]);
studyRoom.RoomName = row["RoomName"].ToString();
studyRoom.RoomCapacity = Convert.ToInt32(row["RoomCapacity"]);
studyRoom.RoomLocation = row["RoomLocation"].ToString();
studyRooms.Add(studyRoom);
}
return studyRooms;
}
public static void AddStudyRoom(StudyRoom studyRoom)
{
string query = string.Format("INSERT INTO StudyRooms (RoomName, RoomCapacity, RoomLocation) VALUES ('{0}', {1}, '{2}')",
studyRoom.RoomName, studyRoom.RoomCapacity, studyRoom.RoomLocation);
DatabaseHelper.ExecuteNonQuery(query);
}
public List<DateTime> GetAvailableTimeSlots(DateTime date)
{
List<DateTime> availableTimeSlots = new List<DateTime>();
string query = string.Format("SELECT StartTime, EndTime FROM RoomReservations WHERE RoomID = {0} AND ReservationDate = '{1}'",
RoomID, date.ToShortDateString());
DataTable dataTable = DatabaseHelper.ExecuteQuery(query);
TimeSpan startTime = new TimeSpan(8, 0, 0);
TimeSpan endTime = new TimeSpan(22, 0, 0);
while (startTime < endTime)
{
DateTime timeSlot = date.Date + startTime;
if (!IsReserved(dataTable, timeSlot))
{
availableTimeSlots.Add(timeSlot);
}
startTime = startTime.Add(new TimeSpan(0, 30, 0));
}
return availableTimeSlots;
}
private bool IsReserved(DataTable dataTable, DateTime timeSlot)
{
foreach (DataRow row in dataTable.Rows)
{
DateTime startTime = DateTime.Parse(row["StartTime"].ToString());
DateTime endTime = DateTime.Parse(row["EndTime"].ToString());
if (timeSlot >= startTime && timeSlot < endTime)
{
return true;
}
}
return false;
}
}
}
```
最后,我们可以在主程序中编写一些代码来测试这个系统的功能。下面是一些示例代码:
```csharp
using System;
using System.Collections.Generic;
namespace StudyRoomReservationSystem
{
class Program
{
static void Main(string[] args)
{
// 添加自习室
StudyRoom.AddStudyRoom(new StudyRoom { RoomName = "Room 1", RoomCapacity = 10, RoomLocation = "Building A" });
StudyRoom.AddStudyRoom(new StudyRoom { RoomName = "Room 2", RoomCapacity = 20, RoomLocation = "Building B" });
// 获取自习室列表
List<StudyRoom> studyRooms = StudyRoom.GetStudyRooms();
foreach (StudyRoom studyRoom in studyRooms)
{
Console.WriteLine("Room {0}: Capacity = {1}, Location = {2}", studyRoom.RoomName, studyRoom.RoomCapacity, studyRoom.RoomLocation);
}
// 查询可用时间段
StudyRoom room1 = studyRooms[0];
DateTime date = DateTime.Today;
List<DateTime> availableTimeSlots = room1.GetAvailableTimeSlots(date);
foreach (DateTime timeSlot in availableTimeSlots)
{
Console.WriteLine("Available time slot: {0}", timeSlot.ToString("yyyy-MM-dd HH:mm"));
}
Console.ReadLine();
}
}
}
```
这是一个简单的自习室预选系统的实现,您可以根据自己的需求进行修改和扩展。
阅读全文