正确使用数据表

时间:2012-04-25 21:04:00

标签: .net powershell datatable oledb

我有一个按需运行的powershell流程,它通过Web服务调用从应用程序收集所有“请求历史记录”日志。结果请求被转换为对象,并且NoteProperty值(属性值对)最终以每周大型列表数组(通常为1400个项目)结束。

我想要做的是将所有这些请求存储起来用于历史目的,以便应用程序不会自行清除它们。因此,我在数据库上创建了一个简单的表,该表存储了我新创建的数据库中尚不存在的每个请求的所有属性值对。

然后我在powershell脚本中将OleDB连接设置为MSSQL服务器,并从表中选择所有记录以填充DataTable(我对OleDB或DataTables不好)。之后,我遍历列表数组中的每个项目以验证它在DataTable中不存在。对于每个不存在的记录,我在DataTable中添加一个具有属性 - 值对的新行。从那里我假设命令构建器帮助Insert语句,所以我不必检查每个属性值,如果它是null或空白,甚至根本不写。最后,我用新添加的DataTable“更新”OleDBAdapter。

虽然这个过程有效,但我意识到它正在做的是从数据库中提取所有数据,然后与我的列表数组进行比较并重新提交新添加的记录。数据库越大,这需要的时间越长。无论如何,无需编写任何SQL语句就可以快速,高效地完成这项工作吗?我喜欢CommandBuilder如何为DataTables工作。

以下是在提取了所有“请求历史记录”对象后调用的函数

function UpdateDatabase([Parameter(Mandatory=$true)] $allRequests)
{
    $objOleDbConnection = New-Object "System.Data.OleDb.OleDbConnection"
    $objOleDbCommand = New-Object "System.Data.OleDb.OleDbCommand"
    $objOleDbAdapter = New-Object "System.Data.OleDb.OleDbDataAdapter"
    $objDataTable = New-Object "System.Data.DataTable"

    $objOleDbConnection.ConnectionString = "Provider=SQLNCLI10;Server=SERVER;Database=DB1;Trusted_Connection=yes;"
    $objOleDbConnection.Open()

    $objOleDbCommand.Connection = $objOleDbConnection
    $objOleDbCommand.CommandText = "SELECT * FROM dbo.RequestLog"

    ##set the Adapter object and command builder
    $objOleDbAdapter.SelectCommand = $objOleDbCommand
    $objOleDbCommandBuilder = New-Object "System.Data.OleDb.OleDbCommandBuilder"
    $objOleDbCommandBuilder.DataAdapter = $objOleDbAdapter

    ##fill the objDataTable object with the results
    [void] $objOleDbAdapter.Fill($objDataTable)
    [void] $objOleDbAdapter.FillSchema($objDataTable,[System.Data.SchemaType]::Source)

    #store all the primary keys in a list for kicking out dups
    $sql_id = @()
    $objDataTable.Rows | foreach { $sql_id += $_.PKID}

    #####

    #loop through all the requests
    trap {
    "Error: $($i)"
    }
    $i = 0
    $total = $allRequests.count
    foreach ($request in $allRequests)
    {       
        $i++
        write-progress -activity "Filling DataTable" -status "% Complete: $($i/$total*100)" -PercentComplete ($i/$total*100)
        #check to see if entry already exists in our table (by primary key)
        if (!($sql_id -contains $request.PKID.Value))
        {
            #shouldn't have to do this but i noticed sometimes requests are duplicate in the list? (probably restarted the script and caught some old requests
            $sql_id += $request.PKID.Value

            $row = $objDataTable.Rows.Add($request.PKID.Value)
            #go through all the attributes from the request and add them to the table
            $list = get-member -in $request | Where-Object { $_.MemberType -eq "NoteProperty" }
            foreach ($attr in $list)
            {
                if ($request.($attr.name) -ne $null)
                {
                    $row.($attr.name) = $request.($attr.name)
                }
            }

        } else { 
            #PKID already in DB
        }

    }
    #update the database with our new records
    $objOleDbAdapter.Update($objDataTable)

    ## close the connection 
    $objOleDbConnection.Close()
}

1 个答案:

答案 0 :(得分:1)

您需要编写一些T-SQL代码才能提高流程效率。您需要将新行发送到SQL Server,以便在SQL Server上进行处理。一种解决方案是使用表值参数,它允许您将DataTable传递给SQL Server。我在这里写了一个例子:

http://sev17.com/2012/04/appending-new-rows-only/