Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

SQL query error when trying to fill dataset.

$
0
0

I keep getting an error when trying to run this SQL query in powershell. I'm guessing it's having a problem converting the dates it pulling into strings. Can someone help me please. 

Exception calling "Fill" with "1" argument(s): "Arithmetic overflow error converting expression to data type datetime."
At C:\Users\administrator.WWC\Desktop\report2.ps1:28 char:26
+ $nRecs = $SqlAdapter.Fill <<<< ($DataSet)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

#Connection Strings
$Database = "test"
$Server = "localhost"
#SMTP Relay Server
$SMTPServer = "smtp.domain.com"
#Export File
$AttachmentPath = "C:\SQLData1.csv"
# Connect to SQL and query data, extract data to SQL Adapter
$sd = 20140501
$ed = 20140529
$SqlQuery =  @"
select s.prod_id PRODUCT, c.BACKEND_ID ROUTE_ID, sum(s.home) HD_DRAW, count(m.ID) COMPLAINT_COUNT 
from supplies s
join customers c on s.DELIVERTO = c.CUST_ID
left outer join complaints m on s.sid=m.sid and s.pubdate=m.pubdate
where s.pubdate between $sd and $ed and s.SID is not NULL
group by s.PROD_ID, c.BACKEND_ID
order by s.prod_id, c.backend_id"@
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=$Server;Initial Catalog=$Database;Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$nRecs = $SqlAdapter.Fill($DataSet)
$nRecs | Out-Null
#Populate Hash Table
$objTable = $DataSet.Tables[0]
#Export Hash Table to CSV File
$objTable | Export-CSV $AttachmentPath
#Send SMTP Message
$Mailer = new-object Net.Mail.SMTPclient($SMTPServer)
$From = "from@domain.com"
$To = "to@domain.com"
$Subject = "Test Subject"
$Body = "Body Test"
$Msg = new-object Net.Mail.MailMessage($From,$To,$Subject,$Body)
$Msg.IsBodyHTML = $False
$Attachment = new-object Net.Mail.Attachment($AttachmentPath)
$Msg.attachments.add($Attachment)
$Mailer.send($Msg)


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>