[英]SQL Count doesn't count number of rows of data in database
我在下面创建了 sub 来计算 SQL 服务器表中的数据出现次数,其中特定值出现在同一个表中。 但是,它不符合我的要求。 子尝试计算表中特定诊所名称的用户数。 但是,它只计为 0。
Private Sub chUser()
conn = New SqlConnection(conStr)
conn.Open()
myConn.clName = clNameDGV.SelectedCells.Item(0).Value
MsgBox(myConn.clName)
Dim comStr As String = "Select Count(*) as clName from Login_Detail Where Clinic_Name = '" & myConn.clName & "'"
Dim comm As New SqlCommand(comStr, conn)
Dim i As Integer = comm.ExecuteScalar()
If i = 0 Then
If MessageBox.Show("No User Information found for '" + clNameDGV.SelectedCells.Item(0).Value + "'." + vbNewLine + "Do you want to enter new user details ?", "No Users Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
Dim nf As New CreateUser
nf.TopMost = True
nf.ShowDialog(Me)
End If
Else
Dim nf1 As New LoginForm
nf1.TopMost = True
nf1.ShowDialog(Me)
End If
conn.Close()
End Sub
试试下面的代码:
Private Sub chUser()
conn = New SqlConnection(conStr)
conn.Open()
myConn.clName = clNameDGV.SelectedCells.Item(0).Value
MsgBox(myConn.clName)
Dim comStr As String = "Select Count(*) as clName from Login_Detail Where Clinic_Name = N'" & myConn.clName & "'"
Dim comm As New SqlCommand(comStr,conn)
//Dim i As Integer = comm.ExecuteScalar()
Dim dt As DataTable
Dim da As New SqlDataAdapter(comm)
da.Fill(dt)
Dim i As Integer = Integer.parse(dt.Rows(0)(0).ToString())
If i = 0 Then
If MessageBox.Show("No User Information found for '" + clNameDGV.SelectedCells.Item(0).Value + "'." + vbNewLine + "Do you want to enter new user details ?", "No Users Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
Dim nf As New CreateUser
nf.TopMost = True
nf.ShowDialog(Me)
End If
Else
Dim nf1 As New LoginForm
nf1.TopMost = True
nf1.ShowDialog(Me)
End If
conn.Close()
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.