Sql server 从PowerShell插入SQL Server表
我有下面的PowerShell脚本,它将从传递的文件夹中获取所有Sql server 从PowerShell插入SQL Server表,sql-server,powershell,Sql Server,Powershell,我有下面的PowerShell脚本,它将从传递的文件夹中获取所有.wav文件,并获取一些信息并导出到.csv文件,我希望能够获得有关插入SQL Server表的帮助 Param($FolderPath) $fulldate = Get-Date -format yyyyMMdd $startPath = Join-Path -Path $FolderPath -ChildPath $fulldate $com = (New-Object -ComObject Shell.App
.wav
文件,并获取一些信息并导出到.csv
文件,我希望能够获得有关插入SQL Server表的帮助
Param($FolderPath)
$fulldate = Get-Date -format yyyyMMdd
$startPath = Join-Path -Path $FolderPath -ChildPath $fulldate
$com = (New-Object -ComObject Shell.Application).NameSpace($startPath)
for($i = 0; $i -lt 64; $i++) {
$name = $com.GetDetailsOf($com.Items, $i)
if ($name -eq 'Length') { $lengthattribute = $i}
}
$com.Items() |
ForEach-Object {
[PSCustomObject]@{
Name = $_.Name
Size = $com.GetDetailsOf($_, 1)
DateCreated = $com.GetDetailsOf($_, 4)
Length = $com.GetDetailsOf($_, $lengthattribute)
}
} | Export-Csv -Path c:\Dell\audiolength.csv -Encoding ascii -NoTypeInformation
Get-Content c:\Dell\audiolength.csv -Encoding UTF8 | % { $_ -replace """","" } | Out-File "c:\Dell\audiolengthCLEAN.csv"
它是这样运行的
.\test.ps1 'c:\Dell\'
现在它正在插入.CSV
,但我想直接插入SQL Server
这是我到目前为止为SQL所做的,但不起作用
Param(
$FolderPath
)
$server = "localhost"
$Database = "Data"
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$fulldate = Get-Date -format yyyyMMdd
$startPath = Join-Path -Path $FolderPath -ChildPath $fulldate
$com = (New-Object -ComObject Shell.Application).NameSpace($startPath)
for($i = 0; $i -lt 64; $i++) {
$name = $com.GetDetailsOf($com.Items, $i)
if ($name -eq 'Length') { $lengthattribute = $i}
}
$com.Items() |
ForEach-Object {
[PSCustomObject]@{
Name = $_.Name
Size = $com.GetDetailsOf($_, 1)
DateCreated = $com.GetDetailsOf($_, 4)
Length = $com.GetDetailsOf($_, $lengthattribute)
} | Select-Object -Property Name,Size,DateCreated,Length
$toto = 'Size'
foreach($i in $com) {
$sql ="
INSERT INTO [dbo].[audiolength]
([Name]
,[Size]
,[DateCreated]
,[Length])
VALUES
('$lengthattribute'
,'$toto'
,'DateCreated'
,'Length')
"
$Command.CommandText = $sql
$Command.ExecuteReader()
}
$Connection.Close()
}
谷歌搜索结果显示:我建议你在那个链接中尝试一些东西,然后带着一个实际的问题回来。你好,我不仅尝试了那个链接,还尝试了其他20个链接,但是没有运气。“没有运气”和“不工作”并不意味着什么。实际问题是什么?i、 你有错误吗?这是怎么一回事?如果没有错误,调试PS脚本时会发生什么。它运行你认为它运行的线路吗?只是猜测,但您可能想使用
ExecuteNonQuery
,而不是ExecuetReader