从我的powershell脚本中,我正在尝试连接到Postgresql数据库。 当我尝试导入Npgsql.dll时,它引发“找不到类型[System.Data.Npgsql.SqlCommand]:验证包含此类型的程序集是否已加载”
Import-Module '.\Npgsql.dll'
#Add-Type -Path '.\Npgsql.dll'
$conn = New-Object System.Data.Npgsql.NpgsqlConnection
$conn.ConnectionString = "Server=**;Port=**;User Id=**;Password=**;Database=**;"
$cmdText = "select * from **"
$conn.Open()
$cmd = New-Object System.Data.Npgsql.SqlCommand($cmdText,$conn)
$rdr = $cmd.ExecuteReader()
答案 0 :(得分:0)
我认为您可能参加的课程是NpgsqlCommand
尝试更改
$cmd = New-Object System.Data.Npgsql.SqlCommand($cmdText,$conn)
收件人
$cmd = New-Object System.Data.Npgsql.NpgsqlSqlCommand($cmdText,$conn)
答案 1 :(得分:0)
此代码适用于 Powershell 7.1.3 和 Npgsql 5.0.4
try
{
Add-type -path "Npgsql.dll"
}
catch { $_.Exception.LoaderExceptions }
$connection = New-Object Npgsql.NpgsqlConnection("User ID=postgres;Password=***;Server=localhost;Port=5432;Database=test;")
try {
$connection.Open()
$cmd = New-Object Npgsql.NpgsqlCommand('INSERT INTO public."Demo" ("A", "B", "C") VALUES (@A, @B, @C)', $connection)
$cmd.Parameters.AddWithValue("A", "Some")
$cmd.Parameters.AddWithValue("B", 123)
$cmd.Parameters.AddWithValue("C", "value")
$cmd.ExecuteNonQuery()
} finally {
$connection.Close()
}