将PowerShell中的值插入存储过程

时间:2019-02-28 13:33:55

标签: sql sql-server powershell stored-procedures

我想将PowerShell脚本中的值放入MS SQL中的存储过程中。 但是当我执行PS脚本时,会发生以下错误:

  

使用0个参数调用“打开”的异常:“ ConnectionString属性未初始化。”

我的PS脚本:

$Pfade = Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\16.0\OneNote\OpenNotebooks" |
         select * -ExcludeProperty PS* |
         Format-List 
$Timestamp = Get-Date -Format g
$User = $env:UserName
$Mailadresse = Get-Aduser $env:UserName -Properties emailaddress |
               select emailaddress |
               Out-String

$SqlConnection.ConnectionString = "server=SGNB18121\MSSQLSERVER,1434;database=Sportverein;User Id=7777777; Password=77777777;"
$QueryText = "exec OneNotePathProcedure" 
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.Open()
$SqlCommand = $SqlConnection.CreateCommand()
$SqlCommand.CommandText = "exec OneNotePathProcedure '$Pfade', 'test', '01/01/2018', '$User'"
Write-Host $SqlCommand.CommandText

$DataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $SqlCommand
$dataset = New-Object System.Data.Dataset
$SqlConnection.Close()

我的SQL:

CREATE PROCEDURE OneNotePathProcedure @Pfad nvarchar(100), @EMail nvarchar(40), @Zeit datetime2, @Benutzer nvarchar(40)
AS 
UPDATE OneNoteTest
SET Pfad = @Pfad, EMail = @EMail, Zeit = @Zeit, Benutzer = @Benutzer
WHERE ID IS NOT NULL;

谁能告诉我为什么会发生此错误?

0 个答案:

没有答案