I have two parameters, from and to date. I need to pass from PowerShell to an SQL stored procedure. I created a GUI to input the parameters in the format of 'YYYY-MM-DD'. However, it's a string, and I need to covert it into sqldatetime in the format of 'YYYY-MM-DD, 00:00:000', so that it can result correctly in SQL.
I tried the below, and it's not working. What did I do wrong?
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=localhost;Database=AMSDataWarehouse test;Integrated Security=SSPI"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.Connection = $SqlConnection
$SqlCmd.CommandText = "YQBreport1"
SqlParameter param1 = new SqlParameter("@from",System.Data.SqlDbType.DateTime);
param1.Value = $from;
SqlParameter param2 = new SqlParameter("@to",System.Data.SqlDbType.DateTime);
param2.Value = $to;
$SqlConnection.Open()
$sqlcmd.ExecuteNonQuery()
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$SQLResult =$DataSet.Tables[0]
$commands = $SQLResult | foreach-object -process { $_.output }> output.ps1
.\output.ps1