如何将多个 Word 文档批量导入 SQL 服务器数据库 table
How to bulk import many Word documents into SQL Server database table
我需要将大约 50,000 个 Word 文档(.doc
和 .docx
)从单个目录导入 SQL Server 2016 数据库 table 以便我可以使用全文索引,然后搜索文档的内容。
由于这是一次性任务,而且不会长期需要数据库,因此我不关心性能或使用 FILESTREAM
或 FileTables 的参数。
我刚刚用一个 table:
创建了一个数据库
CREATE TABLE [dbo].[MyDocument]
(
[ID] INT IDENTITY(1,1) NOT NULL,
[DocumentName] NVARCHAR(255) NOT NULL,
[Extension] NCHAR(10) NOT NULL,
[DocumentContent] VARBINARY(MAX) NOT NULL,
CONSTRAINT [PK_MyDocument] PRIMARY KEY CLUSTERED ([ID] ASC)
)
现在我正在寻找一种方法将我的文档放入 table。网上有很多使用 OPENROWSET
将单个文档导入 SQL 服务器数据库 table 的示例,但是他们要求我为文件指定一个名称,这显然没有用我的要求。
我不敢相信没有一个有据可查的直接方法来做到这一点,但是几个小时的搜索没有找到任何结果,这让我开始怀疑这是否可能,但肯定是?
任何人都可以给我一个用于将多个文件导入数据库的 T-SQL 示例片段吗?或者建议如何实现它?
下面是一个 PowerShell 脚本,它使用参数化查询和 FileStream
参数值将所有“.docx”文件导入指定文件夹,以将文件内容流式传输到数据库,而不是加载整个文件内容进入客户端内存。
# import all documents in specified directory using file stream parameter
try {
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$insertQuery = @"
INSERT INTO dbo.MyDocument (DocumentName, Extension, DocumentContent)
VALUES(@DocumentName, @Extension, @DocumentContent);
"@
$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=.;Initial Catalog=YourDatabase;Integrated Security=SSPI")
$command = New-Object System.Data.SqlClient.SqlCommand($insertQuery, $connection)
$documentNameParameter = $command.Parameters.Add("@DocumentName", [System.Data.SqlDbType]::NVarChar, 255)
$documentExtensionParameter = $command.Parameters.Add("@Extension", [System.Data.SqlDbType]::NVarChar, 10)
$documentContentParameter = $command.Parameters.Add("@DocumentContent", [System.Data.SqlDbType]::VarBinary, -1)
$connection.Open()
$filesToImport = Get-ChildItem "E:\DocumentsToImport\*.docx"
$importedFileCount = 0
foreach($fileToImport in $filesToImport) {
$documentContentStream = [System.IO.File]::Open($fileToImport.FullName, [System.IO.FileMode]::Open)
$documentNameParameter.Value = [System.IO.Path]::GetFileNameWithoutExtension($fileToImport.FullName)
$documentExtensionParameter.Value = [System.IO.Path]::GetExtension($fileToImport.Name)
$documentContentParameter.Value = $documentContentStream
[void]$command.ExecuteNonQuery()
$documentContentStream.Close()
$importedFileCount += 1
}
$connection.Close()
$timer.Stop()
Write-Host "$importedFileCount files imported. Duration $($timer.Elapsed)."
}
catch {
throw
}
我需要将大约 50,000 个 Word 文档(.doc
和 .docx
)从单个目录导入 SQL Server 2016 数据库 table 以便我可以使用全文索引,然后搜索文档的内容。
由于这是一次性任务,而且不会长期需要数据库,因此我不关心性能或使用 FILESTREAM
或 FileTables 的参数。
我刚刚用一个 table:
创建了一个数据库CREATE TABLE [dbo].[MyDocument]
(
[ID] INT IDENTITY(1,1) NOT NULL,
[DocumentName] NVARCHAR(255) NOT NULL,
[Extension] NCHAR(10) NOT NULL,
[DocumentContent] VARBINARY(MAX) NOT NULL,
CONSTRAINT [PK_MyDocument] PRIMARY KEY CLUSTERED ([ID] ASC)
)
现在我正在寻找一种方法将我的文档放入 table。网上有很多使用 OPENROWSET
将单个文档导入 SQL 服务器数据库 table 的示例,但是他们要求我为文件指定一个名称,这显然没有用我的要求。
我不敢相信没有一个有据可查的直接方法来做到这一点,但是几个小时的搜索没有找到任何结果,这让我开始怀疑这是否可能,但肯定是?
任何人都可以给我一个用于将多个文件导入数据库的 T-SQL 示例片段吗?或者建议如何实现它?
下面是一个 PowerShell 脚本,它使用参数化查询和 FileStream
参数值将所有“.docx”文件导入指定文件夹,以将文件内容流式传输到数据库,而不是加载整个文件内容进入客户端内存。
# import all documents in specified directory using file stream parameter
try {
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$insertQuery = @"
INSERT INTO dbo.MyDocument (DocumentName, Extension, DocumentContent)
VALUES(@DocumentName, @Extension, @DocumentContent);
"@
$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=.;Initial Catalog=YourDatabase;Integrated Security=SSPI")
$command = New-Object System.Data.SqlClient.SqlCommand($insertQuery, $connection)
$documentNameParameter = $command.Parameters.Add("@DocumentName", [System.Data.SqlDbType]::NVarChar, 255)
$documentExtensionParameter = $command.Parameters.Add("@Extension", [System.Data.SqlDbType]::NVarChar, 10)
$documentContentParameter = $command.Parameters.Add("@DocumentContent", [System.Data.SqlDbType]::VarBinary, -1)
$connection.Open()
$filesToImport = Get-ChildItem "E:\DocumentsToImport\*.docx"
$importedFileCount = 0
foreach($fileToImport in $filesToImport) {
$documentContentStream = [System.IO.File]::Open($fileToImport.FullName, [System.IO.FileMode]::Open)
$documentNameParameter.Value = [System.IO.Path]::GetFileNameWithoutExtension($fileToImport.FullName)
$documentExtensionParameter.Value = [System.IO.Path]::GetExtension($fileToImport.Name)
$documentContentParameter.Value = $documentContentStream
[void]$command.ExecuteNonQuery()
$documentContentStream.Close()
$importedFileCount += 1
}
$connection.Close()
$timer.Stop()
Write-Host "$importedFileCount files imported. Duration $($timer.Elapsed)."
}
catch {
throw
}