c#将第一次测试数据(测试总数,ok数,ng数,合格率)保存到数据库,下此开启软件,数据显示到相应的组件中,继续测试,数据继续增加更新,关闭软件时,测试数据更新为第二次的测试数据,再次开启软件,显示更新后的数据,以此类推(注意,是将数据更新,不是插入)
时间: 2024-05-14 08:18:46 浏览: 13
实现这个功能,需要使用一个数据库来存储测试数据。假设你已经创建了一个名为"TestResults"的数据库,并且在其中创建了一个名为"Results"的表,用于存储测试数据。这个表包含以下列:
- ID (int, 主键, 自增)
- TestDate (datetime, 测试日期)
- TotalTests (int, 总测试数)
- OKCount (int, OK 数量)
- NGCount (int, NG 数量)
- PassRate (float, 合格率)
下面是实现这个功能的步骤:
1. 定义一个名为 "TestResult" 的类,用于表示测试结果的数据结构。
```csharp
public class TestResult {
public int TotalTests { get; set; }
public int OKCount { get; set; }
public int NGCount { get; set; }
public float PassRate { get; set; }
}
```
2. 在程序启动时,从数据库中获取最新的测试结果,并将其显示在相应的组件中。
```csharp
private void Form_Load(object sender, EventArgs e) {
TestResult result = GetLatestResultFromDatabase();
UpdateUI(result);
}
private TestResult GetLatestResultFromDatabase() {
string connectionString = "your connection string";
string query = "SELECT TOP 1 TotalTests, OKCount, NGCount, PassRate FROM Results ORDER BY TestDate DESC";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read()) {
TestResult result = new TestResult {
TotalTests = reader.GetInt32(0),
OKCount = reader.GetInt32(1),
NGCount = reader.GetInt32(2),
PassRate = reader.GetFloat(3)
};
return result;
}
return null;
}
}
private void UpdateUI(TestResult result) {
if (result != null) {
totalTestsLabel.Text = result.TotalTests.ToString();
okCountLabel.Text = result.OKCount.ToString();
ngCountLabel.Text = result.NGCount.ToString();
passRateLabel.Text = result.PassRate.ToString("P");
}
}
```
3. 在测试完成后,将测试结果保存到数据库中。
```csharp
private void SaveTestResultToDatabase(TestResult result) {
string connectionString = "your connection string";
string query = "INSERT INTO Results (TestDate, TotalTests, OKCount, NGCount, PassRate) VALUES (@TestDate, @TotalTests, @OKCount, @NGCount, @PassRate)";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@TestDate", DateTime.Now);
command.Parameters.AddWithValue("@TotalTests", result.TotalTests);
command.Parameters.AddWithValue("@OKCount", result.OKCount);
command.Parameters.AddWithValue("@NGCount", result.NGCount);
command.Parameters.AddWithValue("@PassRate", result.PassRate);
command.ExecuteNonQuery();
}
}
```
4. 在程序关闭时,获取最新的测试结果,并将其保存到数据库中。
```csharp
private void Form_Closing(object sender, FormClosingEventArgs e) {
TestResult result = GetCurrentResult();
SaveTestResultToDatabase(result);
}
private TestResult GetCurrentResult() {
TestResult result = new TestResult {
TotalTests = int.Parse(totalTestsLabel.Text),
OKCount = int.Parse(okCountLabel.Text),
NGCount = int.Parse(ngCountLabel.Text),
PassRate = float.Parse(passRateLabel.Text.TrimEnd('%')) / 100
};
return result;
}
```
这样,每次打开程序时,就可以自动加载最新的测试结果,并在测试完成后将其保存到数据库中。这个过程中,测试数据会被更新而不是插入。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)