1

我需要计算 sql 数据库中的不同行,然后结果将存储在 Int 变量中

例如计算 tblSales(table) 中的不同行(SalesID) 并将其存储在 NUM(int variable) 中。

蒂亚:)

    Public Function ExecuteQuery(ByVal query As String) As DataTable
        Try
           Dim cn As New SqlConnection(con)
           Dim da As New SqlDataAdapter(query, cn)
           Dim cb As New SqlCommandBuilder(da)
           Dim dt As New DataTable
           da.Fill(dt)

           Return dt
        Catch ex As Exception
           MessageBox.Show(ex.Message)
           Return Nothing
        End Try
    End Function

    Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click 
       dim count as string = "select distinct count(SalesID) from tblSales"  
       ExecuteQuery(count) 
       dim Num as Integer = Int32.Parse(Val(count)) 
    End Sub
4

1 回答 1

2

你可以试试这样的

Dim num As Integer
Dim sql As String
sql = "select count(distinct SalesID) from tblSales"
Using con As New SqlConnection(connStr)
    Using com As New SqlCommand(sql, con)
        con.Open()
        num = Convert.ToInt32(com.ExecuteScalar())
    End Using
End Using
于 2013-10-30T07:01:30.180 回答