【发布时间】:2017-01-11 13:27:57
【问题描述】:
我正在使用 Powershell 4 并尝试将数据写入 SQL Server 2012。
这是我正在使用的脚本
Add-Type -AssemblyName System.Data
$conn = New-Object System.Data.SqlClient.SqlConnection↵
$conn.ConnectionString = "Data Source=<SQLSERVER>;Initial Catalog=SYSINFO;Integrated Security=true;"
$conn.open()
$cmd = New-Object System.Data.SqlClient.SqlCommand
我得到的错误是:
New-Object : Cannot find type [System.Data.SqlClient.SqlConnection] : verify that the assembly containing this type is loaded.
我假设第一行 (Add-Type) 将加载 System.Data 下的所有必需程序集
我是否遗漏了一些明显的东西?
【问题讨论】:
-
能不能用-PassThru看看是否真的加了SQLConnection类型?类似于 Add-Type -AssemblyName "System.Data" -PassThru | ForEach-Object { if($_.Name -eq "SqlConnection") { $_ } }
-
Invoke-Sqlcmd 可能更容易。