0

I have a winform application and im using sql local db but i see a slow performance on loading records from database and when laptop on standby mode and i start using application i see the database close and very very slow to load data and im using this code to load data

 Sub loadcart()
        Dim _total As Double
        Dim _stotal As Double

        DataGridView1.Rows.Clear()
        cn.Open()

        cm = New SqlCommand("Select c.id, p.description, c.price, c.qty, c.total,c.sprice, c.stotal from tblcart as c inner join tblproduct as p on p.id = c.pid Where c.transno like '" & LblTransNo.Text & "' ", cn)
        dr = cm.ExecuteReader
        While dr.Read
            _total += CDbl(dr.Item("total").ToString)
            _stotal += CDbl(dr.Item("stotal").ToString)
            DataGridView1.Rows.Add(dr.Item("id").ToString, dr.Item("description").ToString, dr.Item("price").ToString, dr.Item("qty").ToString, dr.Item("total").ToString, dr.Item("sprice").ToString, dr.Item("stotal").ToString)
        End While
        dr.Close()
        cn.Close()
        LblTotal.Text = Format(_total, "#,##0.00")
        LblSTotal.Text = Format(_stotal, "#,##0.00")
        'LblTotal.Text = Format(_total, "#,##0.00") & " DA"
        If DataGridView1.Rows.Count < 1 Then
            BtnSettle.Enabled = False
            'BtnCancel.Enabled = False
        Else
            BtnSettle.Enabled = True
            'BtnCancel.Enabled = True

        End If
    End Sub
Charlieface
  • 52,284
  • 6
  • 19
  • 43
Chaker
  • 59
  • 6
  • Takes me ages to get my work done when my laptop's in standby too.. (Did you mean "after I wake the computer up from sleep, queries are slow, but they get quicker after some time"?) – Caius Jard Apr 12 '22 at 09:56
  • @CaiusJard Yes i mean after i wake the laptop up but its very very slow to load data i think the connection shutdown after the standby mode – Chaker Apr 12 '22 at 10:04
  • Avoid keeping a global connection; make a new one when you need it – Caius Jard Apr 12 '22 at 10:05
  • Database can be large and take a lot of memory and disk space. When you do not have a lot of memory or run on a slow disk or run on machine where disk space is fully utilized the database will be slow. – jdweng Apr 12 '22 at 10:14
  • @jdweng no its very simple database with 100 records max on loading – Chaker Apr 12 '22 at 10:24
  • Check the amount of free space on disk and memory usage on task manager. – jdweng Apr 12 '22 at 10:47
  • @ all it good the space and the memory – Chaker Apr 12 '22 at 10:58
  • Side points: your code could really do with improvement. Do not cache connection object, create it when needed. Dispose connection, command and reader/adapter with `Using`. Use parameterized queries, do **not** inject data into your query. – Charlieface Apr 12 '22 at 12:50

0 Answers0