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 19:05:31 浏览: 141
可以在计算通过率之前,检查 `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
```
这样,就更加安全和可靠了。
运用OleDbDataAdapter解决上述问题
使用 OleDbDataAdapter 对象可以通过连接到 OLE DB 数据源来检索和更新数据。以下是一个基本的 VB.NET 代码片段展示如何使用 OleDbDataAdapter 对象从数据库中搜索数据并将结果返回:
```vb
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Persist Security Info=False;"
Dim query As String = "SELECT * FROM myTable WHERE column1 = @value1"
Using conn As New OleDbConnection(connString)
conn.Open()
Using adapter As New OleDbDataAdapter(query, conn)
adapter.SelectCommand.Parameters.AddWithValue("@value1", "searchValue")
Dim dataSet As New DataSet()
adapter.Fill(dataSet, "myTable")
Dim dataTable As DataTable = dataSet.Tables("myTable")
For Each row As DataRow In dataTable.Rows
'处理每一行数据的逻辑
Next
End Using
End Using
```
在这个例子中,我们连接到了一个 Microsoft Access 数据库,并使用 `SELECT` 语句搜索名为 `myTable` 的表中所有符合 `column1 = @value1` 条件的行。 `@value1` 是一个参数,可以通过调用 `AddWithValue` 方法来动态设置。然后,我们使用 `OleDbDataAdapter` 对象执行查询,并将结果填充到一个 `DataSet` 对象中。最后,我们遍历了搜索结果中的每一行,并在每一行上执行了处理逻辑。
你可以根据自己的具体需求来调整查询语句和处理逻辑。
阅读全文