帮我生成一个基于sql sever数据库,C#语言的一个宿舍管理系统
时间: 2024-03-16 10:47:29 浏览: 142
好的,以下是一个基于 SQL Server 数据库、使用 C# 语言实现的宿舍管理系统的示例代码:
1. 首先创建一个名为 DormitoryManagement 的 C# 控制台应用程序。
2. 在项目中添加对 System.Data.SqlClient 的引用,以便能够使用 SQL Server 数据库。
3. 在 SQL Server 中创建名为 Dormitory 的数据库,然后创建如下的宿舍表:
```sql
CREATE TABLE dbo.Dorm (
ID INT PRIMARY KEY IDENTITY(1,1),
RoomNo VARCHAR(50) NOT NULL,
BedCount INT NOT NULL,
OccupiedBedCount INT NOT NULL DEFAULT 0
)
```
4. 在 C# 代码中使用以下命名空间:
```c#
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
```
5. 在 C# 代码中定义一个名为 Dormitory 的类,它包含以下成员:
```c#
class Dormitory
{
private const string ConnectionString = "Data Source=(local);Initial Catalog=Dormitory;Integrated Security=True";
private const string SelectAllSql = "SELECT * FROM Dorm";
private const string InsertSql = "INSERT INTO Dorm (RoomNo, BedCount, OccupiedBedCount) VALUES (@RoomNo, @BedCount, @OccupiedBedCount)";
private const string UpdateSql = "UPDATE Dorm SET RoomNo = @RoomNo, BedCount = @BedCount, OccupiedBedCount = @OccupiedBedCount WHERE ID = @ID";
private const string DeleteSql = "DELETE FROM Dorm WHERE ID = @ID";
public int ID { get; set; }
public string RoomNo { get; set; }
public int BedCount { get; set; }
public int OccupiedBedCount { get; set; }
public static List<Dormitory> GetAll()
{
var result = new List<Dormitory>();
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (var command = new SqlCommand(SelectAllSql, connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var dorm = new Dormitory
{
ID = (int)reader["ID"],
RoomNo = (string)reader["RoomNo"],
BedCount = (int)reader["BedCount"],
OccupiedBedCount = (int)reader["OccupiedBedCount"]
};
result.Add(dorm);
}
}
}
}
return result;
}
public static void Insert(Dormitory dorm)
{
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (var command = new SqlCommand(InsertSql, connection))
{
command.Parameters.AddWithValue("@RoomNo", dorm.RoomNo);
command.Parameters.AddWithValue("@BedCount", dorm.BedCount);
command.Parameters.AddWithValue("@OccupiedBedCount", dorm.OccupiedBedCount);
command.ExecuteNonQuery();
}
}
}
public static void Update(Dormitory dorm)
{
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (var command = new SqlCommand(UpdateSql, connection))
{
command.Parameters.AddWithValue("@ID", dorm.ID);
command.Parameters.AddWithValue("@RoomNo", dorm.RoomNo);
command.Parameters.AddWithValue("@BedCount", dorm.BedCount);
command.Parameters.AddWithValue("@OccupiedBedCount", dorm.OccupiedBedCount);
command.ExecuteNonQuery();
}
}
}
public static void Delete(int id)
{
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (var command = new SqlCommand(DeleteSql, connection))
{
command.Parameters.AddWithValue("@ID", id);
command.ExecuteNonQuery();
}
}
}
}
```
6. 在 C# 代码的 Main 方法中添加以下代码,以便测试宿舍管理系统的功能:
```c#
static void Main(string[] args)
{
// 获取所有宿舍
var dorms = Dormitory.GetAll();
Console.WriteLine("所有宿舍:");
foreach (var dorm in dorms)
{
Console.WriteLine($"ID: {dorm.ID}, RoomNo: {dorm.RoomNo}, BedCount: {dorm.BedCount}, OccupiedBedCount: {dorm.OccupiedBedCount}");
}
Console.WriteLine();
// 添加新宿舍
var newDorm = new Dormitory
{
RoomNo = "101",
BedCount = 4,
OccupiedBedCount = 2
};
Dormitory.Insert(newDorm);
Console.WriteLine("添加新宿舍后的所有宿舍:");
dorms = Dormitory.GetAll();
foreach (var dorm in dorms)
{
Console.WriteLine($"ID: {dorm.ID}, RoomNo: {dorm.RoomNo}, BedCount: {dorm.BedCount}, OccupiedBedCount: {dorm.OccupiedBedCount}");
}
Console.WriteLine();
// 修改宿舍信息
var firstDorm = dorms[0];
firstDorm.RoomNo = "102";
firstDorm.BedCount = 3;
Dormitory.Update(firstDorm);
Console.WriteLine("修改宿舍信息后的所有宿舍:");
dorms = Dormitory.GetAll();
foreach (var dorm in dorms)
{
Console.WriteLine($"ID: {dorm.ID}, RoomNo: {dorm.RoomNo}, BedCount: {dorm.BedCount}, OccupiedBedCount: {dorm.OccupiedBedCount}");
}
Console.WriteLine();
// 删除宿舍
var lastDorm = dorms[dorms.Count - 1];
Dormitory.Delete(lastDorm.ID);
Console.WriteLine("删除宿舍后的所有宿舍:");
dorms = Dormitory.GetAll();
foreach (var dorm in dorms)
{
Console.WriteLine($"ID: {dorm.ID}, RoomNo: {dorm.RoomNo}, BedCount: {dorm.BedCount}, OccupiedBedCount: {dorm.OccupiedBedCount}");
}
Console.ReadKey();
}
```
这个示例程序实现了一个简单的宿舍管理系统,它可以获取所有宿舍、添加新宿舍、修改宿舍信息和删除宿舍。你可以根据自己的需求在此基础上进行修改和扩展。
阅读全文