用C#连接数据库 创建疫苗表(编号、名称、注射量、保质期、价格); 添加5条测试数据 实现增删改查
时间: 2024-01-31 17:04:53 浏览: 62
以下是用C#连接数据库创建疫苗表(编号、名称、注射量、保质期、价格);添加5条测试数据并实现增删改查的示例代码:
```csharp
using System;
using System.Data.SqlClient;
namespace VaccineManagement
{
class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=VaccineDB;Integrated Security=True;";
// 创建疫苗表
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string createTableQuery = "CREATE TABLE Vaccine (Id INT PRIMARY KEY IDENTITY, Name VARCHAR(50), Dosage INT, ShelfLife INT, Price DECIMAL(10, 2))";
SqlCommand command = new SqlCommand(createTableQuery, connection);
command.ExecuteNonQuery();
}
// 添加测试数据
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string insertQuery = "INSERT INTO Vaccine (Name, Dosage, ShelfLife, Price) VALUES (@Name, @Dosage, @ShelfLife, @Price)";
SqlCommand command = new SqlCommand(insertQuery, connection);
command.Parameters.AddWithValue("@Name", "疫苗A");
command.Parameters.AddWithValue("@Dosage", 2);
command.Parameters.AddWithValue("@ShelfLife", 365);
command.Parameters.AddWithValue("@Price", 100);
command.ExecuteNonQuery();
command.Parameters.Clear();
command.Parameters.AddWithValue("@Name", "疫苗B");
command.Parameters.AddWithValue("@Dosage", 3);
command.Parameters.AddWithValue("@ShelfLife", 180);
command.Parameters.AddWithValue("@Price", 200);
command.ExecuteNonQuery();
command.Parameters.Clear();
command.Parameters.AddWithValue("@Name", "疫苗C");
command.Parameters.AddWithValue("@Dosage", 1);
command.Parameters.AddWithValue("@ShelfLife", 90);
command.Parameters.AddWithValue("@Price", 50);
command.ExecuteNonQuery();
command.Parameters.Clear();
command.Parameters.AddWithValue("@Name", "疫苗D");
command.Parameters.AddWithValue("@Dosage", 4);
command.Parameters.AddWithValue("@ShelfLife", 365);
command.Parameters.AddWithValue("@Price", 300);
command.ExecuteNonQuery();
command.Parameters.Clear();
command.Parameters.AddWithValue("@Name", "疫苗E");
command.Parameters.AddWithValue("@Dosage", 2);
command.Parameters.AddWithValue("@ShelfLife", 180);
command.Parameters.AddWithValue("@Price", 150);
command.ExecuteNonQuery();
}
// 查询疫苗信息
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string selectQuery = "SELECT * FROM Vaccine";
SqlCommand command = new SqlCommand(selectQuery, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"编号: {reader["Id"]}, 名称: {reader["Name"]}, 注射量: {reader["Dosage"]}, 保质期: {reader["ShelfLife"]}, 价格: {reader["Price"]}");
}
reader.Close();
}
// 更新疫苗信息
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string updateQuery = "UPDATE Vaccine SET Price = @Price WHERE Name = @Name";
SqlCommand command = new SqlCommand(updateQuery, connection);
command.Parameters.AddWithValue("@Name", "疫苗B");
command.Parameters.AddWithValue("@Price", 250);
int rowsAffected = command.ExecuteNonQuery();
Console.WriteLine($"{rowsAffected} 行受影响");
}
// 查询更新后的疫苗信息
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string selectQuery = "SELECT * FROM Vaccine WHERE Name = @Name";
SqlCommand command = new SqlCommand(selectQuery, connection);
command.Parameters.AddWithValue("@Name", "疫苗B");
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"编号: {reader["Id"]}, 名称: {reader["Name"]}, 注射量: {reader["Dosage"]}, 保质期: {reader["ShelfLife"]}, 价格: {reader["Price"]}");
}
reader.Close();
}
// 删除疫苗信息
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string deleteQuery = "DELETE FROM Vaccine WHERE Name = @Name";
SqlCommand command = new SqlCommand(deleteQuery, connection);
command.Parameters.AddWithValue("@Name", "疫苗D");
int rowsAffected = command.ExecuteNonQuery();
Console.WriteLine($"{rowsAffected} 行受影响");
}
// 查询删除后的疫苗信息
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string selectQuery = "SELECT * FROM Vaccine";
SqlCommand command = new SqlCommand(selectQuery, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"编号: {reader["Id"]}, 名称: {reader["Name"]}, 注射量: {reader["Dosage"]}, 保质期: {reader["ShelfLife"]}, 价格: {reader["Price"]}");
}
reader.Close();
}
}
}
}
```
注意,上述示例代码使用的数据库为本地 SQL Server Express LocalDB 实例,需要先安装 SQL Server Express LocalDB,并且连接字符串中的 Initial Catalog 参数指定了创建的数据库名称为 VaccineDB。如果使用其他数据库或者 SQL Server 实例,请相应更改连接字符串。
阅读全文