conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT sum_count, pass_count, fail_count FROM test_result", conn); DataTable dt = new DataTable(); adapter.Fill(dt); conn.Close(); if (dt.Rows.Count > 0) { totalCount = Convert.ToInt32(dt.Rows[0]["sum_count"]); okCount = Convert.ToInt32(dt.Rows[0]["pass_count"]); ngCount = Convert.ToInt32(dt.Rows[0]["fail_count"]); double passRate = (double)okCount / totalCount; txt_passrate.Text = passRate.ToString("P2"); //passRate = Convert.ToInt32(dt.Rows[0]["pass_rate"]); txt_sumcount.Text = " " + totalCount.ToString(); txt_passcount.Text = " " + okCount.ToString(); txt_failcount.Text = " " + ngCount.ToString(); //txt_passrate.Text = " " + passRate.ToString(); },以上代码中,当okcount,ngcount,totalcount数据都清零时,下次打开软件,txt_passrate.Text组件显示NAN,怎么让它显示0%
时间: 2023-06-19 09:05:31 浏览: 43
可以在计算通过率之前,检查 `totalCount` 是否为零,如果为零,则直接将通过率设置为0,如下所示:
```
if (dt.Rows.Count > 0) {
totalCount = Convert.ToInt32(dt.Rows[0]["sum_count"]);
okCount = Convert.ToInt32(dt.Rows[0]["pass_count"]);
ngCount = Convert.ToInt32(dt.Rows[0]["fail_count"]);
if (totalCount == 0) {
txt_passrate.Text = "0%";
} else {
double passRate = (double)okCount / totalCount;
txt_passrate.Text = passRate.ToString("P2");
}
txt_sumcount.Text = " " + totalCount.ToString();
txt_passcount.Text = " " + okCount.ToString();
txt_failcount.Text = " " + ngCount.ToString();
}
```
这样,当 `totalCount` 为零时,就会直接将 `txt_passrate.Text` 设置为 "0%"。
相关问题
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim xh As String Dim tqd As String xh = Val(TextBox1.Text) tqd = Val(TextBox2.Text) Dim conn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\Andrew\OneDrive\文件\研究生\油水\油水选型软件\Database1.mdb" Using connection As New OleDbConnection(conn) ' 打开连接 connection.Open() ' 创建 SQL 查询语句 Dim query As String = "SELECT xh FROM Sheet1" ' 创建命令对象 Using command As New OleDbCommand(query, connection) ' 添加参数 ' command.Parameters.AddWithValue("@param1", "%" & searchTerm & "%") ' 创建数据适配器 Using adapter As New OleDbDataAdapter(command) ' 创建数据集 Dim dataSet As New DataSet() ' 填充数据集 adapter.Fill(dataSet, "Sheet1") ' 将数据集绑定到 DataGridView 控件 DataGridView1.DataSource = dataSet.Tables("myTable") End Using End Using ' 关闭连接 connection.Close() End Using 优化这段代码
这段代码可以进行如下优化:
1. 使用 Try-Catch-Finally 块来确保连接、命令和适配器等对象的正确关闭。
2. 使用参数化查询来避免 SQL 注入攻击。
3. 使用 Using 语句来确保所有对象都能够正确释放。
下面是优化后的代码:
```
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xh As String = TextBox1.Text
Dim tqd As String = TextBox2.Text
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\Andrew\OneDrive\文件\研究生\油水\油水选型软件\Database1.mdb"
Using conn As New OleDbConnection(connString)
Try
conn.Open()
Dim query As String = "SELECT xh FROM Sheet1 WHERE xh = @xh AND tqd = @tqd"
Using cmd As New OleDbCommand(query, conn)
cmd.Parameters.AddWithValue("@xh", xh)
cmd.Parameters.AddWithValue("@tqd", tqd)
Using adapter As New OleDbDataAdapter(cmd)
Dim dataSet As New DataSet()
adapter.Fill(dataSet, "Sheet1")
DataGridView1.DataSource = dataSet.Tables("Sheet1")
End Using
End Using
Catch ex As Exception
MessageBox.Show("An error occurred: " & ex.Message)
Finally
conn.Close()
End Try
End Using
End Sub
```
这样,就更加安全和可靠了。
winform excel导入mysql_c#将Excel数据导入到数据库的实现代码
以下是将 Excel 数据导入到 MySQL 数据库的 C# 代码示例:
```csharp
private void btnImport_Click(object sender, EventArgs e)
{
// 获取 Excel 文件路径
string filePath = txtFilePath.Text;
// 连接 Excel 文件
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;'";
OleDbConnection conn = new OleDbConnection(connStr);
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
// 将 Excel 数据读入 DataSet
DataSet ds = new DataSet();
adapter.Fill(ds);
// 连接 MySQL 数据库
string mysqlConnStr = "Server=127.0.0.1;Database=test;Uid=root;Pwd=123456;";
MySqlConnection mysqlConn = new MySqlConnection(mysqlConnStr);
mysqlConn.Open();
// 插入数据到 MySQL 数据库
foreach (DataRow row in ds.Tables[0].Rows)
{
MySqlCommand mysqlCmd = new MySqlCommand();
mysqlCmd.Connection = mysqlConn;
mysqlCmd.CommandText = "INSERT INTO `test`.`table1` (`Column1`, `Column2`, `Column3`) VALUES (@Column1, @Column2, @Column3)";
mysqlCmd.Parameters.AddWithValue("@Column1", row["Column1"]);
mysqlCmd.Parameters.AddWithValue("@Column2", row["Column2"]);
mysqlCmd.Parameters.AddWithValue("@Column3", row["Column3"]);
mysqlCmd.ExecuteNonQuery();
}
MessageBox.Show("导入成功!");
}
catch (Exception ex)
{
MessageBox.Show("导入失败:" + ex.Message);
}
finally
{
conn.Close();
mysqlConn.Close();
}
}
```
需要注意的是,该示例代码使用了 `OleDbConnection` 和 `OleDbDataAdapter` 来连接 Excel 文件和读取数据,使用了 `MySqlConnection` 和 `MySqlCommand` 来连接 MySQL 数据库和插入数据。在使用前需要引入 `System.Data.OleDb` 和 `MySql.Data.MySqlClient` 命名空间,并且需要先安装 `MySql.Data` NuGet 包。另外,代码中的数据库连接字符串、Excel 表名、表字段名等需要根据实际情况进行调整。