简体   繁体   English

vb.net 多查询一张表

[英]vb.net multiple queries one table

I'm using VS to build an app that queries an access db.我正在使用 VS 构建一个查询访问数据库的应用程序。 In one of the form tabs there are 12 buttons and near each button a label were it shows the number of pressed times.在其中一个表单选项卡中有 12 个按钮,每个按钮附近都有一个标签,用于显示按下的次数。 When the form loads it queries the DB to show the number of times the user pressed in that day.当表单加载时,它会查询 DB 以显示用户当天按下的次数。

Public Class Form1
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\networkdrive\DB_Reg_V01_10.accdb"
Dim da1, da2, da3, da4, da5, da6, da7, da8, da9, da10, da11, da12 As OleDbDataAdapter
Dim ds1, ds2, ds3, ds4, ds5, ds6, ds7, ds8, ds9, ds10, ds11, ds12 As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
Dim provider, dataFile As String
Dim cmd As OleDbCommand = Nothing
Dim myConnection As OleDbConnection = New OleDbConnection

Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load

ds1 = New DataSet
tables = ds1.Tables
da1 = New OleDbDataAdapter("Select Count(*) from TB_Atividades Where ([User] = '" & boxUser.Text & "') AND (Data = DATE()) AND (Prod = 'Cat23')", myConnection)
da1.Fill(ds1, "ID")
Dim view1 As New DataView(tables(0))
DataGridView1.DataSource = view1
lblCount1.Text = DataGridView1.CurrentCell.Value.ToString()

ds2 = New DataSet
tables = ds2.Tables
da2 = New OleDbDataAdapter("Select Count(*) from TB_Atividades Where ([User] = '" & boxUser.Text & "') AND (Data = DATE()) AND (Prod = 'Cat4410')", myConnection)
da2.Fill(ds2, "ID")
Dim view1 As New DataView(tables(0))
DataGridView1.DataSource = view2
lblCount2.Text = DataGridView1.CurrentCell.Value.ToString()

/.../

ds12 = New DataSet
tables = ds12.Tables
da12 = New OleDbDataAdapter("Select Count(*) from TB_Atividades Where ([User] = '" & boxUser.Text & "') AND (Data = DATE()) AND (Prod = 'Cat101')", myConnection)
da12.Fill(ds12, "ID")
Dim view12 As New DataView(tables(0))
DataGridView1.DataSource = view12
lblCount12.Text = DataGridView1.CurrentCell.Value.ToString()

/.../

The code is working but it takes some time to start.代码正在运行,但需要一些时间才能启动。 I just what to make it more efficient.我只是想使它更有效率。

It's there a way to reduce the number of call's to the DB?有没有办法减少对数据库的调用次数?

Thnks in advance.提前谢谢。

You need to learn how to do functions and parametrize your query .您需要学习如何执行函数和参数化您的查询 Your connection need to be open for the shorted amount of time.您的连接需要在较短的时间内打开。 If you just need the count, you just need to call ExecuteScalar instead of having a whole dataset.如果您只需要计数,您只需要调用ExecuteScalar而不是拥有整个数据集。

Function GetCount(ByVal user As String, ByVal product As String) As Integer

    Dim result As Integer
    Dim command As New OleDbCommand("Select Count(*) from TB_Atividades Where ([User] = ?) AND (Data = DATE()) AND (Prod = ?)", connection)

    command.Parameters.Add(user)
    command.Parameters.Add(product)

    myConnection.Open()
    result = command.ExecuteScalar()
    myConnection.Close()

    return result
End Function


lblCount1.Text = GetCount(boxUser.Text, "Cat23")
lblCount2.Text = GetCount(boxUser.Text, "Cat4410")

I can't test this so don't just copy/paste it.我无法测试这个,所以不要只是复制/粘贴它。 I hope this will give you guidance to take the correct steps.我希望这将为您提供指导以采取正确的步骤。

If you need to query all categories, then just return a list of all your category and loop through them.如果您需要查询所有类别,则只需返回所有类别的列表并循环遍历它们。 A better to query the database the least amount of time.最好以最少的时间查询数据库。

SELECT Prod, count(*) FROM ... GROUP BY Prod

If you are going to use all your logics in Form_Load event then it will take some time to complete its operation which results in delay of form loading.如果您打算在 Form_Load 事件中使用所有逻辑,那么完成其操作将需要一些时间,从而导致表单加载延迟。

In order to avoid that you need to implement Threading concept in your application.为了避免这种情况,您需要在应用程序中实现线程概念。 You need to create a new thread and write the logics inside it.您需要创建一个新线程并在其中编写逻辑。 Start the thread in Form_Load event and display a loading symbol in your form until execution of thread is over.在 Form_Load 事件中启动线程并在表单中显示加载符号,直到线程执行结束。

Instead of querying the database often, you can fetch all the records from the database and store it in a DataTable.您可以从数据库中获取所有记录并将其存储在 DataTable 中,而不是经常查询数据库。 use DataTable.select("your query") to filter out the records based on your requirement.使用 DataTable.select("your query") 根据您的要求过滤掉记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM