将VB.Net SQL Count语句转换为标签

时间:2017-03-16 12:11:18

标签: sql vb.net

我试图计算那些教师teacher = '" & lblTeacher.Text & "'"

的学生

示例:

enter image description here

Public Class Form1
Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Richard\Desktop\Dbase.mdb"
Dim con As New OleDbConnection
Dim da, da1 As New OleDbDataAdapter
Dim dt, dt1 As New DataTable
Dim sql As String
Dim ds As New DataSet

Public Sub display()
    sql = "select * from Info"
    dt.Clear()
    con.Open()
    da = New OleDbDataAdapter(sql, con)
    da.Fill(dt)
    con.Close()
    DataGridView1.DataSource = dt.DefaultView
End Sub
Public Sub count()
    sql = "select COUNT(name) from Info where teacher = '" & lblTeacher.Text & "'"
    da1 = New OleDbDataAdapter(sql, con)
    ds.Clear()
    con.Open()
    da.Fill(ds)
    lblCount.Text = ds.Tables(0).Rows.Count.ToString
    con.Close()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    con.ConnectionString = conn
    display()
End Sub

Private Sub DataGridView1_Click(sender As System.Object, e As System.EventArgs) Handles DataGridView1.Click
    lblTeacher.Text = DataGridView1.CurrentRow.Cells("teacher").Value.ToString
    count()
End Sub
End Class

1

1 个答案:

答案 0 :(得分:1)

尝试使用此方法代替当前的count()方法。特别注意我的意见;他们从原始代码中解决了一些不良做法:

' Better functional style: accept a value, return the result
Public Function GetStudentCount(teacher As String) As Integer
    '**NEVER** use string concatenation to put data into an SQL command!!!
    Const sql As String = "select COUNT(name) from Info where teacher =  ?"

    'Don't try to re-use the same connection in your app.
    '  It creates a bottleneck, and breaks ADO.Net's built-in connection pooling,
    '  meaning it's more likely to make object use *worse*, rather than better.
    'Additionally, connection objects should be created in a Using block,
    '  so they will still be closed if an exception is thrown.
    '  The original code would have left the connection hanging open.
    Using con As New OleDbConnection(conn), _
          cmd As New OleDbCommand(sql, con)

        'This, rather than string concatenation, is how you should put a value into your sql command
        'Note that this NEVER directly replaces the "?" character with the parameter value,
        '   even in the database itself. The command and the data are always kept separated.
        cmd.Parameters.Add("teacher", OleDbType.VarChar).Value = teacher

        con.Open()
        '    No need to fill a whole dataset, just to get one integer back
        Return DirectCast(cmd.ExecuteScalar(), Integer)

       'No need to call con.Close() manually. The Using block takes care of it for you.
    End Using
End Function

再次,没有所有额外的评论:

Public Function GetStudentCount(teacher As String) As Integer
    Const sql As String = "select COUNT(name) from Info where teacher =  ?"

    Using con As New OleDbConnection(conn), _
          cmd As New OleDbCommand(sql, con)
        cmd.Parameters.Add("teacher", OleDbType.VarChar).Value = teacher           
        con.Open()
        Return DirectCast(cmd.ExecuteScalar(), Integer)
    End Using
End Function

这样称呼:

Private Sub DataGridView1_Click(sender As System.Object, e As System.EventArgs) Handles DataGridView1.Click
    lblTeacher.Text = DataGridView1.CurrentRow.Cells("teacher").Value.ToString()
    lblCount.Text = GetStudentCount(lblTeacher.Text).ToString()
End Sub