0

嗨,我正在尝试更新列,但似乎不起作用。它提取数据并在字符串中进行日期转换,但我下面代码的更新部分不想插入 Powershell 所做的更改。

## connect to db and do stuff 
$Connection = new-object system.data.SqlClient.SQLConnection("Data Source=       (local);Integrated Security=SSPI;Initial Catalog=TESTDB");
$sqlqry = "select row_id, DestFileName from FL_Input Where DestFileName like '%[0-9] '     + 'Jan%' + ' [0-9]%'" 
$Command = new-object system.data.sqlclient.sqlcommand 
$Command.CommandText = $sqlqry 
$Command.Connection = $Connection 
$Connection.Open()  

$UpdateCmd = new-object System.Data.SqlClient.SqlCommand 
$UpdateCmd.Connection = $Connection 

## for each filename, update timestamp - this replaces your $file in $files loop. 
$Result = $Command.ExecuteReader() 
while ($Result.Read()) { 

$destfile = $Result['DestFileName'] 
$srcfile = $destfile 
$row_id = $Result['row_id'] 

## do all your regex stuff here 

$destfile -match '\d{2}\s\w+\s\d{2,4}' | Out-Null <# Test #>  
$destfile -match '\d{2}\-\w+\-\d{4}' | Out-Null <# Test #>  

$destFile -replace "$(($matches).values)" , "$(get-date "$(($matches).Values)" -Format     yyyyMMdd)"   

write-host "{0}->{1}" -f  $destfile, $srcfile

# when you're finished doing your regex, push the result result back to the db: 
$updateqry = "update FL_Input set DestFileName='{0}' WHERE Row_ID = {1} " -f $destfile, $row_id 
$UpdateCmd.CommandText = $updateqry 
$UpdateCmd.ExecuteNonQuery() 

} 

## all done 
$Connection.Close
4

1 回答 1

0

您是否缺少 row_id 周围的单引号,例如:

$updateqry = "update FL_Input set DestFileName='$destfile' WHERE Row_ID = '$row_id'"
于 2013-10-01T03:48:31.170 回答