使用同一打开的连接插入50多个查询

omvjsjqw  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(313)

所以我基本上想要的是在我的数据库中插入一个以上的查询,而不需要conn.close比conn.open太多次,也不需要尝试50多次。。
以下是代码:

Dim COMMAND As MySqlCommand
    Dim reader As MySqlDataReader
    Dim conn As MySqlConnection
    Dim Item1 As String = "INSERT INTO item_template (entry, name, display) VALUES ('1234', 'Testing', '654';"
    Dim Item3 As String = "UPDATE Item_template SET entry = '123' where name like 'test32111';"
    Dim Item2 As String = "INSERT INTO item_template (entry, name, display) VALUES ('123467', 'Testing332', '65478';"
    Try
        conn.Open()
        COMMAND = New MySqlCommand(Item1, conn)
        reader = COMMAND.ExecuteReader
        conn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Try
        conn.Open()
        COMMAND = New MySqlCommand(Item3, conn)
        reader = COMMAND.ExecuteReader
        conn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Try
        conn.Open()
        COMMAND = New MySqlCommand(Item2, conn)
        reader = COMMAND.ExecuteReader
        conn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
blmhpbnm

blmhpbnm1#

您可以进行一些优化。您应该在using语句中嵌入可支配资源。using语句会自动关闭并在最后处理资源。尤其是连接会自动关闭。即使using块由于异常或return语句终止该方法而提前离开,它也会这样做。
处理批处理时保持连接打开。
干:不要重复你自己。尽量避免代码重复,使复制粘贴。在下面的示例中,我创建了一个可以在循环中处理的sql文本数组。
insert、update、delete等sql命令不返回结果集。使用 ExecuteNonQuery 而不是 ExecuteReader 为了他们。
可以通过为命令分配新的命令文本来恢复该命令。

Dim sqlTexts = {
    "INSERT INTO item_template (entry, name, display) VALUES ('1234', 'Testing', '654';",
    "UPDATE Item_template SET entry = '123' where name like 'test32111';",
    "INSERT INTO item_template (entry, name, display) VALUES ('12367', 'Testing332', '65478';"
}

Using _
    conn As New MySqlConnection(connectionString),
    command As New MySqlCommand With {
        .CommandType = CommandType.Text,
        .Connection = conn
    }

    conn.Open()
    For Each sql As String In sqlTexts
        Try
            command.CommandText = sql
            command.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    Next
End Using

注意,using语句包括 command 以及我们正在使用vb的隐式行继续功能作为初始化 sqlTexts 数组。

fafcakar

fafcakar2#

如果不解决参数化/准备查询等其他问题,最简单的解决方法应该是。。。

Dim COMMAND As MySqlCommand
Dim conn As MySqlConnection
Dim Item1 As String = "INSERT INTO item_template (entry, name, display) VALUES ('1234', 'Testing', '654');"
Dim Item3 As String = "UPDATE Item_template SET entry = '123' where name like 'test32111';"
Dim Item2 As String = "INSERT INTO item_template (entry, name, display) VALUES ('123467', 'Testing332', '65478');"
Try
    conn.Open()
    COMMAND = New MySqlCommand("", conn)
    COMMAND.CommandText = Item1
    COMMAND.ExecuteNonQuery
    COMMAND.CommandText = Item2
    COMMAND.ExecuteNonQuery
    COMMAND.CommandText = Item3
    COMMAND.ExecuteNonQuery
    conn.Close()
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

我的vb有点生疏,所以要小心打字错误(我试图添加 ; (不止一次)

相关问题