Gestione import date
This commit is contained in:
parent
803e6ae9dc
commit
a969485fd7
@ -51,17 +51,27 @@ try {
|
|||||||
$columns = $data[0].PSObject.Properties.Name
|
$columns = $data[0].PSObject.Properties.Name
|
||||||
$columnList = ($columns | ForEach-Object { "[$_]" }) -join ","
|
$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
|
# Process each row
|
||||||
foreach ($row in $data) {
|
foreach ($row in $data) {
|
||||||
# Create value list for this row
|
# Create value list for this row
|
||||||
$values = @()
|
$values = @()
|
||||||
foreach ($column in $columns) {
|
foreach ($column in $columns) {
|
||||||
$value = $row.$column
|
$value = $row.$column
|
||||||
if ([string]::IsNullOrEmpty($value)) {
|
$values += Format-SqlValue $value
|
||||||
$values += "NULL"
|
|
||||||
} else {
|
|
||||||
$values += "'" + $value.Replace("'", "''") + "'"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create and execute insert query for this row
|
# Create and execute insert query for this row
|
||||||
|
Loading…
x
Reference in New Issue
Block a user