我正在编写一个一遍又一遍地需要多个 sql 连接的脚本。我需要并行性来加快速度。这就是为什么我想要一种返回连接句柄的 SQL 工厂。但不知何故,我的代码不起作用。我在这里做错了什么?
$m = New-Module -Name sql_factory -AsCustomObject -ScriptBlock {
Function new_session {
$db_host = 'my.sqlhost.tld';
$db_user = 'user';
$db_pass = 'pass';
$db_name = 'db';
$table_name = $env:COMPUTERNAME;
$conn = New-Object system.data.sqlclient.sqlconnection;
$conn.ConnectionString = "Server=${db_host};Database=${db_name};User ID=${db_user};Password=${db_pass};";
$cmd = New-Object System.Data.SqlClient.SqlCommand;
$cmd.connection = $conn;
return $cmd, $conn;
}
}
$test = {
$cmd, $conn = $m.new_session();
$conn.Open();
$cmd.CommandText = "INSERT INTO tableXXX (date, time, action, protocol, src_ip, dst_ip, src_port, dst_port, size, tcp_flags, tcpsyn, tcpack, tcpwin, icmptype, icmpcode, info, path) VALUES ('xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx')";
$cmd.ExecuteNonQuery();
$conn.Close();
}
start-job -ScriptBlock $test | Out-Null
get-job | receive-job -AutoRemoveJob -Wait
这是我收到的错误消息...
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : localhost
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : localhost
Property 'CommandText' cannot be found on this object; make sure it exists and is settable.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
+ PSComputerName : localhost
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : localhost
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : localhost