用c#编写宠物注册登陆系统
时间: 2024-03-17 18:46:52 浏览: 34
好的,宠物注册登录系统可以分为以下几个模块:
1. 用户注册模块
在用户注册模块中,需要让用户输入宠物的相关信息,包括宠物的名称、种类、年龄、性别等等。同时,要求用户输入一个用户名和密码,这样用户就可以使用自己的账号来登录系统。在用户输入完信息后,将这些信息保存到数据库中。
2. 用户登录模块
在用户登录模块中,用户需要输入自己的用户名和密码,系统会根据这些信息去数据库中验证用户的身份。如果验证通过,系统就会让用户进入宠物管理界面。
3. 宠物管理模块
在宠物管理模块中,用户可以对自己的宠物进行管理,包括添加宠物、修改宠物信息、删除宠物等等。同时,用户也可以查看自己所有的宠物信息。
下面是一个用C#语言编写的简单的宠物注册登录系统的示例代码:
```
using System;
using System.Data.SqlClient;
namespace PetRegisterSystem
{
class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=YOUR_SERVER_NAME;Initial Catalog=YOUR_DATABASE_NAME;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
Console.WriteLine("欢迎使用宠物注册登录系统!");
Console.WriteLine("请输入用户名:");
string username = Console.ReadLine();
Console.WriteLine("请输入密码:");
string password = Console.ReadLine();
int userId = CheckUser(connection, username, password);
if (userId != -1)
{
Console.WriteLine("登录成功!");
Console.WriteLine("请选择您要进行的操作:");
Console.WriteLine("1. 添加宠物");
Console.WriteLine("2. 修改宠物信息");
Console.WriteLine("3. 删除宠物");
Console.WriteLine("4. 查看宠物信息");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
AddPet(connection, userId);
break;
case 2:
UpdatePet(connection, userId);
break;
case 3:
DeletePet(connection, userId);
break;
case 4:
ShowPetInfo(connection, userId);
break;
default:
Console.WriteLine("输入有误,请重新选择!");
break;
}
}
else
{
Console.WriteLine("用户名或密码错误,请重新输入!");
}
connection.Close();
}
static int CheckUser(SqlConnection connection, string username, string password)
{
string query = $"SELECT Id FROM Users WHERE Username='{username}' AND Password='{password}'";
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
int userId = reader.GetInt32(0);
reader.Close();
return userId;
}
else
{
reader.Close();
return -1;
}
}
static void AddPet(SqlConnection connection, int userId)
{
Console.WriteLine("请输入宠物的名称:");
string name = Console.ReadLine();
Console.WriteLine("请输入宠物的种类:");
string breed = Console.ReadLine();
Console.WriteLine("请输入宠物的年龄:");
int age = int.Parse(Console.ReadLine());
Console.WriteLine("请输入宠物的性别:");
string gender = Console.ReadLine();
string query = $"INSERT INTO Pets (Name, Breed, Age, Gender, UserId) VALUES ('{name}', '{breed}', {age}, '{gender}', {userId})";
SqlCommand command = new SqlCommand(query, connection);
int result = command.ExecuteNonQuery();
if (result == 1)
{
Console.WriteLine("添加宠物成功!");
}
else
{
Console.WriteLine("添加宠物失败!");
}
}
static void UpdatePet(SqlConnection connection, int userId)
{
Console.WriteLine("请输入要修改的宠物的名称:");
string name = Console.ReadLine();
Console.WriteLine("请输入新的宠物的名称:");
string newName = Console.ReadLine();
Console.WriteLine("请输入新的宠物的种类:");
string newBreed = Console.ReadLine();
Console.WriteLine("请输入新的宠物的年龄:");
int newAge = int.Parse(Console.ReadLine());
Console.WriteLine("请输入新的宠物的性别:");
string newGender = Console.ReadLine();
string query = $"UPDATE Pets SET Name='{newName}', Breed='{newBreed}', Age={newAge}, Gender='{newGender}' WHERE Name='{name}' AND UserId={userId}";
SqlCommand command = new SqlCommand(query, connection);
int result = command.ExecuteNonQuery();
if (result == 1)
{
Console.WriteLine("修改宠物信息成功!");
}
else
{
Console.WriteLine("修改宠物信息失败!");
}
}
static void DeletePet(SqlConnection connection, int userId)
{
Console.WriteLine("请输入要删除的宠物的名称:");
string name = Console.ReadLine();
string query = $"DELETE FROM Pets WHERE Name='{name}' AND UserId={userId}";
SqlCommand command = new SqlCommand(query, connection);
int result = command.ExecuteNonQuery();
if (result == 1)
{
Console.WriteLine("删除宠物成功!");
}
else
{
Console.WriteLine("删除宠物失败!");
}
}
static void ShowPetInfo(SqlConnection connection, int userId)
{
string query = $"SELECT Name, Breed, Age, Gender FROM Pets WHERE UserId={userId}";
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = command.ExecuteReader();
Console.WriteLine("您的宠物信息如下:");
while (reader.Read())
{
string name = reader.GetString(0);
string breed = reader.GetString(1);
int age = reader.GetInt32(2);
string gender = reader.GetString(3);
Console.WriteLine($"名称:{name},种类:{breed},年龄:{age},性别:{gender}");
}
reader.Close();
}
}
}
```
需要注意的是,这里的 `YOUR_SERVER_NAME` 和 `YOUR_DATABASE_NAME` 需要替换为你自己的SQL Server的名称和数据库名称。另外,这里的代码只是一个示例,实际应用中还需要进行更多的错误处理和数据验证。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)