gpt4 book ai didi

mysql - PowerShell 和 MySQL - 保持连接打开以进行多个查询?

转载 作者:行者123 更新时间:2023-11-29 20:53:02 25 4
gpt4 key购买 nike

我正在将 MySQL 连接器与 PowerShell 一起使用,并且需要执行查询,然后稍微清理一下数据,然后将剩余部分转储到同一 MySQL 数据库上的另一个表。我怎样才能保持连接打开以使这一切运行得更快一点?

[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$Connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$Connection.ConnectionString = $ConnectionString
$Connection.Open()

$Command = New-Object MySql.Data.MySqlClient.MySqlCommand($Query, $Connection)
$DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($Command)
$DataSet = New-Object System.Data.DataSet
$RecordCount = $dataAdapter.Fill($dataSet, "data")
return $DataSet.Tables[0]

最佳答案

在调用该函数的函数中创建连接对象。被调用函数将继承调用函数所维护的任何对象,因此 $Connection 在被调用函数中将有效。

function RunMe(){
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$Connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$Connection.ConnectionString = $ConnectionString
$Connection.Open()

# Call the function that executes the MySQL code
ExecuteMySQL

# If needed, call another function that executes the MySQL code
ExecuteSomeOtherMySQL

# Close the connection after all MySQL operations are complete
$Connection.Close()
}

function ExecuteMySQL(){
$Command = New-Object MySql.Data.MySqlClient.MySqlCommand($Query, $Connection)
$DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($Command)
$DataSet = New-Object System.Data.DataSet
$RecordCount = $dataAdapter.Fill($dataSet, "data")
return $DataSet.Tables[0]
}

关于mysql - PowerShell 和 MySQL - 保持连接打开以进行多个查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37866378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com