Gestione import date

This commit is contained in:
Gaetano Savo 2025-06-06 14:08:55 +02:00
parent 803e6ae9dc
commit a969485fd7

View File

@ -51,17 +51,27 @@ try {
$columns = $data[0].PSObject.Properties.Name
$columnList = ($columns | ForEach-Object { "[$_]" }) -join ","
# Function to format value for SQL
function Format-SqlValue {
param($value)
if ([string]::IsNullOrEmpty($value)) {
return "NULL"
}
# Try to parse as date
[datetime]$dateValue = [datetime]::MinValue
if ([datetime]::TryParse($value, [ref]$dateValue)) {
return "'" + $dateValue.ToString("yyyy-MM-dd HH:mm:ss") + "'"
}
return "'" + $value.Replace("'", "''") + "'"
}
# Process each row
foreach ($row in $data) {
# Create value list for this row
$values = @()
foreach ($column in $columns) {
$value = $row.$column
if ([string]::IsNullOrEmpty($value)) {
$values += "NULL"
} else {
$values += "'" + $value.Replace("'", "''") + "'"
}
$values += Format-SqlValue $value
}
# Create and execute insert query for this row