使用PowerShell从SQL Server导出到json文本文件

时间:2018-11-28 23:44:53

标签: json powershell powershell-v4.0

如果满足以下条件,则应阅读以下内容:您正试图找出如何将SQL Server数据转换为JSON并将其放入文本.json文件中的方法

问题:

有人可以告诉我这段代码有什么问题吗?我的目标是从SQL Server表中读取数据,将其转换为JSON,然后将结果另存为JSON文本文件。代码可以运行,但是生成的.json文件只有:

{
        "FieldCount":  11
    },
    {

一遍又一遍地重复,仅此而已。

我的代码:

$instance = "localhost\SQLEXPRESS"
$connectionString = "Server=$Instance; Database=myDB;Integrated Security=True;"
$query = "Select  * from myTable"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
$result | ConvertTo-Json | Out-File "file.json"

$connection.Close()

更新

从技术上讲,他/她将回答我的原始问题(尽管我会警告并说我还没有尝试过),因此会授予postanote答案。

但是,我建议使用BCP来回答迈克的答案或最终得到的答案:

bcp "select * from myTable FOR JSON AUTO" queryout "C:\filepath\testsml.json"  -c -S ".\SQLEXPRESS" -d myDBName -T

3 个答案:

答案 0 :(得分:1)

如果您使用的是SQL Server Express 2016或更高版本,则应该能够使用FOR JSON子句在数据库端进行操作。尝试类似

$instance = "localhost\SQLEXPRESS"
$connectionString = "Server=$Instance; Database=myDB;Integrated Security=True;"
$query = "Select  * from myTable FOR JSON AUTO"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$command = $connection.CreateCommand()
$command.CommandText = $query
$command.ExecuteScalar() | Out-File "file.json"

答案 1 :(得分:0)

尝试这样的事情……

### Exporting SQL Server table to JSON

Clear-Host

#--Establishing connection to SQL Server --# 

$InstanceName = "."
$connectionString = "Server=$InstanceName;Database=msdb;Integrated Security=True;"

#--Main Query --# 

$query = "SELECT * FROM sysjobs"

$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$connection.Open()
$command = $connection.CreateCommand()
$command.CommandText = $query

$result = $command.ExecuteReader()

$table = new-object "System.Data.DataTable"

$table.Load($result)

#--Exporting data to the screen --# 

$table | select $table.Columns.ColumnName | ConvertTo-Json

$connection.Close()

# Results

{
    "job_id":  "5126aca3-1003-481c-ab36-60b45a7ee757",
    "originating_server_id":  0,
    "name":  "syspolicy_purge_history",
    "enabled":  1,
    "description":  "No description available.",
    "start_step_id":  1,
    "category_id":  0,
    "owner_sid":  [
                      1
                  ],
    "notify_level_eventlog":  0,
    "notify_level_email":  0,
    "notify_level_netsend":  0,
    "notify_level_page":  0,
    "notify_email_operator_id":  0,
    "notify_netsend_operator_id":  0,
    "notify_page_operator_id":  0,
    "delete_level":  0,
    "date_created":  "\/Date(1542859767703)\/",
    "date_modified":  "\/Date(1542859767870)\/",
    "version_number":  5
}

答案 2 :(得分:0)

此处的“ rub”是,即使具有执行标量的SQL命令List<>也会截断JSON输出,而使用FOR JSON AUTO输出到变量仍然会截断。如果重要,请使用与Visual Studio捆绑在一起的SQL 2016 LocalDB。