简体   繁体   English

VB.net动态参数用于MySql查询

[英]VB.net Dynamic Parameters for MySql query

Here I am again, asking for some help. 我再次在这里寻求帮助。

I have a code that is working fine for inserting a record in my table 我有一个代码可以很好地在表中插入记录

Dim insert4 = "insert into f_logs(f_id, log_Date,log_action,log_Destination,log_status,log_account,log_remarks) " _
& " values(@id,@date,@destination,@destination2,@status,@account,@remarks)"
cmd3 = New MySqlCommand(insert4, conn)


/* cadd is my function for adding values to the parameters */
/* The function simplifies the adding of parameter*/
/* x.Parameters.AddWithValue(para, val) */

cadd(cmd3, "@id", docu_id)
cadd(cmd3, "@date", ToMySql(dt1.Value))
cadd(cmd3, "@destination", get_destination())
cadd(cmd3, "@destination2", tbDestination.Text)
cadd(cmd3, "@status", get_status())
cadd(cmd3, "@account", tbAccount.Text)
cadd(cmd3, "@remarks", tbRemarks.Text)

cmd3.ExecuteNonQuery()

/* cpara is a function for clearing the parameters*/
/* The function simplifies the clearing of parameter */
/* since i am using it frequently*/
cpara(cmd3)



Here comes the problem. 问题来了。 I need to create the logs for each record in my table. 我需要为表中的每个记录创建日志。 I fetch the needed id's for the logs using this code: 我使用以下代码获取日志所需的ID:

SELECT f_id FROM gsis_new.filelocation t2 " _
& " where t2.f_location =@loc"

And then i put the result in a hidden datagridview. 然后将结果放入隐藏的datagridview中。


I could use a loop to insert all the logs generated for each id, but executing each query one by one will take took to long to finish specially when I am inserting like 100 logs. 我可以使用循环插入为每个id生成的所有日志,但是特别是当我插入100条日志时,逐个执行每个查询将花费很长时间才能完成。

/* Like using this logic in adding logs */

    Start
        Add value to parameters
        Execute Query
        clear parameters
    Repeat


That is why i want to insert all the logs in a single query, but i do not know how to do that. 这就是为什么我想在一个查询中插入所有日志,但是我不知道该怎么做。 I needed to add parameters in my query during run-time. 我需要在运行时在查询中添加参数。

/* I want to use this logic in adding logs */
Add parameters that is equal to the number of logs to be inserted.

Start
  Add value to the parameters   
Repeat 

Execute Query
clear parameters

---------- ----------

UPDATE: 更新:

I tried to insert it one by using this code: 我试图通过使用以下代码将其插入一个:

Dim count As Integer = (dgMatchID.Rows.Count() - 1)

            Try

                Dim watch As New Stopwatch()

                If count > 0 Then
                    cadd(cmd3, "@id", "0")
                    cadd(cmd3, "@date", ToMySql(dt1.Value))
                    cadd(cmd3, "@destination", get_destination())
                    cadd(cmd3, "@destination2", tbDestination.Text)
                    cadd(cmd3, "@status", get_status())
                    cadd(cmd3, "@account", tbAccount.Text)
                    cadd(cmd3, "@remarks", tbRemarks.Text)

                    For i As Integer = 0 To count - 1
                        cmd3.Parameters("@id").Value = dgMatchID.Rows(i).Cells("f_id").Value.ToString
                        cmd3.ExecuteNonQuery()
                        watch.Start()

                    Next
                    watch.Stop()
                    Dim seconds As Double = watch.Elapsed.TotalSeconds()
                    MsgBox(seconds)
                Else
                    MsgBox("Nothing to insert!!!")
                End If

And it takes 8.0429649 seconds to complete the insertion of 215 rows. 完成215行的插入需要8.0429649秒。 Is there any way to shorten the execution time, I am thinking that if I only execute the MySQLCommand once, the time it need will be shorten. 有什么办法可以缩短执行时间,我在想,如果我只执行一次MySQLCommand,它将缩短所需的时间。

If you are using .NET 4+ you can insert using a Parallel Foreach statement. 如果使用的是.NET 4+,则可以使用Parallel Foreach语句插入。 This will execute in parallel which will insert several records at the same time. 这将并行执行,这将同时插入多个记录。 This should be faster. 这应该更快。 I am unable to test this but functionally it should work. 我无法对此进行测试,但是在功能上应该可以。

Dim Options As ParallelOptions = New ParallelOptions With {.MaxDegreeOfParallelism = 100}

The Options above is not needed you could just remove this all together. 不需要上面的选项,您可以将它们全部删除。 Removing will let .NET decide how many threads to run based on resources. 删除将使.NET根据资源确定要运行的线程数。 You would need to make sure your MaxPool is high enough to take advantage of the multiple connections that will be opened 您需要确保MaxPool足够高,以利用将要打开的多个连接

Parallel.ForEach(dgMatchID.Rows.Cast(Of DataGridViewRow), Options, 
Function()

        Dim insert4 As String = "insert into f_logs(f_id, log_Date,log_action,log_Destination,log_status,log_account,log_remarks) " _
                                & " values(@id,@date,@destination,@destination2,@status,@account,@remarks)"

        Dim conn As New MySqlConnection()
        Dim cmd3 As MySqlCommand = New MySqlCommand(insert4, conn)

        cmd3.Parameters.AddWithValue("@id", "")
        cmd3.Parameters.AddWithValue("@date", "")
        cmd3.Parameters.AddWithValue("@destination", "")
        cmd3.Parameters.AddWithValue("@destination2", "")
        cmd3.Parameters.AddWithValue("@status", "")
        cmd3.Parameters.AddWithValue("@account", "")
        cmd3.Parameters.AddWithValue("@remarks", "")

        Return New With {
                    Key .Conn = conn,
                    Key .Cmd = cmd3
                }

    End Function,
    Function(dr, state, localInit)

        Dim row As DataGridViewRow = dr
        localInit.Cmd.Parameters("@id").Value = row.Cells("f_id").Value.ToString
        localInit.Cmd.ExecuteNonQuery()

        Return localInit

    End Function,
    Function(localInit)

        localInit.Cmd.Dispose()
        localInit.Conn.Dispose()

        Return localInit

    End Function)

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

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