我编写了一个非常简单的脚本,它从文件和文件夹中收集数据,并将其上传到 SQL 数据库。我相信我的问题与参数化 sql的问题有关,但我不明白如何或为什么。
我认为我需要做的是重新格式化 sql 字符串以防止某些字符进入。
任何帮助表示赞赏。
这是代码:
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $dbConnection
$Command.CommandText = "INSERT INTO FileSizeTable (FileName,FileSize,FileNameLength,Date) VALUES ('$i','$items','$temp','$currentDate')"
$Command.ExecuteNonQuery()
"INSERT INTO FileSizeTable (FileName,FileSize,FileNameLength,Date) VALUES ('$i','$items','$temp','$currentDate')"
这是输出(我将 sql 命令字符串作为测试推出):
INSERT INTO FileSizeTable (FileName,FileSize,FileNameLength,Date) VALUES ('ATI Te
chnologies','61.16 MB','39','05/24/2013 21:05:56')
ATI Technologies 61.16 MB 39
1
INSERT INTO FileSizeTable (FileName,FileSize,FileNameLength,Date) VALUES ('ATIToo
l','0.00 MB','30','05/24/2013 21:05:56')
ATITool 0.00 MB 30
1
INSERT INTO FileSizeTable (FileName,FileSize,FileNameLength,Date) VALUES ('Auran'
,'7,496.04 MB','28','05/24/2013 21:05:56')
Auran 7,496.04 MB 28
Exception calling "ExecuteNonQuery" with "0" argument(s): "Incorrect syntax near
's'.
Unclosed quotation mark after the character string ')'."
At line:143 char:25
+ $Command.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException