为什么我的应用程序运行缓慢,是否有任何代码可以使该应用程序运行得更快?

知道 Gyuy

我正在制作一个应用程序来查找数据网格的值是否在表 1 中,然后在表 2 上更新数据网格填充是表 2 我的代码正在运行,但需要 1 分钟或更长时间才能完成 1 行,是否有任何代码可以使我的应用程序运行得更快?

这是我的代码:

Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim sql As String = "select date,npk,nama,masuk,out,h_ot,flag,status from lemburapril"
    Dim dataadapter As New SqlDataAdapter(sql, str)
    str.Open()
    Dim ds As New DataSet()
    dataadapter.Fill(ds, "ds")
    str.Close()
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "ds"
    For i = 0 To ds.Tables(0).Rows.Count - 1

        Dim tgl As Date = ds.Tables(0).Rows(i).Item(0).ToString
        Dim npk As String = ds.Tables(0).Rows(i).Item(1).ToString
        Dim nama As String = ds.Tables(0).Rows(i).Item(2).ToString
        Dim masuk As String = ds.Tables(0).Rows(i).Item(3).ToString
        Dim out As String = ds.Tables(0).Rows(i).Item(4).ToString
        Dim h_ot As String = ds.Tables(0).Rows(i).Item(5).ToString
        Dim Flag As String = ds.Tables(0).Rows(i).Item(6).ToString
        Dim status As String = ds.Tables(0).Rows(i).Item(7).ToString
        For Each Rows In DataGridView1.Rows
            Dim sqlup As String = "update lemburapril set status = 'v' where date = any (select date from ABY1904$ where date ='" & tgl & "') and npk = any (select npk from ABY1904$ where npk = '" & npk & "')"
            str.Open()
            COMMANDSQL(sqlup)
            str.Close()
        Next
    Next
End Sub
Public Sub display()
    Dim sql As String = "select date,npk,nama,masuk,out,h_ot,flag,status from lemburapril"
    Dim dataadapter As New SqlDataAdapter(Sql, str)
    str.Open()
    Dim ds As New DataSet()
    dataadapter.Fill(ds, "ds")
    str.Close()
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "ds"
End Sub

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

这是我的连接:

Module koneksi
Dim sqlcom As New SqlCommand
Dim commansql As String

Public str As New SqlConnection("data source= Somebody-PC\SQLEXPRESS ;initial catalog= lembur ;integrated security =true")
Public Sub COMMANDSQL(ByVal S As String)
    sqlcom = New SqlCommand(S, str)
    sqlcom.CommandType = CommandType.Text
    sqlcom.ExecuteNonQuery()
End Sub
End Module
玛丽

摆脱模块。将所有数据库对象保持在本地,以便您可以关闭和处置它们。即使出现错误,Using...End Using 块也会处理这个问题。看起来您正在遍历所有行并为嵌套 for 中的每一行再次运行更新。这会给你 n*n ,这会减慢速度。

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

Private ConnString As String = "data source= Somebody-PC\SQLEXPRESS ;initial catalog= lembur ;integrated security =true"

Private dt As New DataTable

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Using Str As New SqlConnection(ConnString)
        Using cmd As New SqlCommand("Update lemburapril Set status = 'v' Where date = any (Select date From ABY1904$ Where date =@tgl) And npk = any (Select npk From ABY1904$ Where npk = @npk)", Str)
            cmd.Parameters.Add("@tgl", SqlDbType.Date)
            cmd.Parameters.Add("@npk", SqlDbType.VarChar)
            Str.Open()
            For i = 0 To dt.Rows.Count - 1
                cmd.Parameters("@tgl").Value = CDate(dt.Rows(i).Item(0))
                cmd.Parameters("@npk").Value = dt.Rows(i).Item(1).ToString
                cmd.ExecuteNonQuery()
            Next
        End Using
    End Using
End Sub
Public Sub display()
    Using Str As New SqlConnection(ConnString)
        Using cmd As New SqlCommand("select date,npk,nama,masuk,[out],h_ot,flag,status from lemburapril", Str)
            Str.Open()
            dt.Load(cmd.ExecuteReader)
        End Using
    End Using
    DataGridView1.DataSource = dt
End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

与命令行相比,为什么我的应用程序在IntelliJ中运行得更快?

当进程数大于python中CPU的线程总数时,为什么应用程序可以运行得更快?

如何使我的应用程序运行更快

从最近使用的应用程序清除后,是否可以使应用程序在后台运行?

如果我应用断点并以调试模式运行该应用程序,则我的应用程序可以完美运行

为什么该应用程序无法在我的手机上运行?

为什么我的PyGame应用程序根本不运行?

为什么删除迁移运行我的应用程序?

为什么我不能运行WatchOS 2应用程序?

为什么LaunchAgents无法运行我的Automator应用程序?

有什么方法可以使用管理员权限运行UWP应用程序?

有什么方法可以使用触发器运行 Apps Script Web 应用程序

运行应用程序时,应用程序崩溃。我不知道为什么?

有什么方法可以在我的Android应用程序布局中运行其他应用程序吗?

为什么我使用Gunicorn运行Flask应用程序,似乎“没有名为应用程序的模块”

我的Android应用程序运行非常缓慢

使用 aws fargate 的缺点(缺点)是什么,为什么我的 Laravel 应用程序在 fargate 与弹性 beantalk 上运行缓慢?

为什么我的 SKShapeNode 子类在我运行我的应用程序时没有出现?

我可以配置Spring MVC应用程序,以便每当我运行该应用程序时,都应自动调用控制器或任何方法

添加代码后,为什么我的Android应用程序崩溃而没有任何错误?

Heroku没有运行我的python应用程序,但是该应用程序在本地运行良好

即使没有路径变量,为什么我也可以从Windows cmd启动某些应用程序并运行?

为什么我的python Slack应用程序永远运行而什么也没有发生?

react native-我是否应该使用react-native start命令运行服务器,以便用户可以使用该应用程序?

在Tizen Web应用程序中,我可以使用javascript Web Worker在关闭的应用程序上运行后台代码吗?

为什么我的应用程序没有一个较旧的设备上运行?

是否有任何图像应用程序可以裁剪比GIMP更快/更亮的图像?

我如何压缩我的XML和Java代码以使我的应用程序运行更流畅

我的GAE应用程序运行缓慢,日志中有很多“来自未知来源的HTTP(503)”