| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| param( |
| [string] $dataSource = ".\SQLEXPRESS", |
| [string] $database = "Northwind", |
| [string] $sqlCommand = $(throw "Please specify a query."), |
| [System.Management.Automation.PsCredential] $credential |
| ) |
| |
| |
| |
| |
| $authentication = "Integrated Security=SSPI;" |
| |
| |
| |
| if($credential) |
| { |
| $plainCred = $credential.GetNetworkCredential() |
| $authentication = |
| ("uid={0};pwd={1};" -f $plainCred.Username,$plainCred.Password) |
| } |
| |
| |
| |
| $connectionString = "Provider=sqloledb; " + |
| "Data Source=$dataSource; " + |
| "Initial Catalog=$database; " + |
| "$authentication; " |
| |
| |
| |
| if($dataSource -match '\.xls$|\.mdb |
| |
| 呵呵,里面都已经有了详细的解释如何使用,就不需要我多做解释了。我用这段脚本查询过sql数据库和access数据库,另外它还可以用来查询excel文档!真的很棒! |
| |
| http://bbs.winos.cn/thread-28867-1-1.html) |
| { |
| $connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$dataSource; " |
| |
| if($dataSource -match '\.xls |
| |
| 呵呵,里面都已经有了详细的解释如何使用,就不需要我多做解释了。我用这段脚本查询过sql数据库和access数据库,另外它还可以用来查询excel文档!真的很棒! |
| |
| http://bbs.winos.cn/thread-28867-1-1.html) |
| { |
| $connectionString += 'Extended Properties="Excel 8.0;"; ' |
| |
| |
| if($sqlCommand -notmatch '\[.+\$\]') |
| { |
| $error = 'Sheet names should be surrounded by square brackets, and ' + |
| 'have a dollar sign at the end: [Sheet1$]' |
| Write-Error $error |
| return |
| } |
| } |
| } |
| |
| |
| $connection = New-Object System.Data.OleDb.OleDbConnection $connectionString |
| $command = New-Object System.Data.OleDb.OleDbCommand $sqlCommand,$connection |
| $connection.Open() |
| |
| |
| $adapter = New-Object System.Data.OleDb.OleDbDataAdapter $command |
| $dataset = New-Object System.Data.DataSet |
| [void] $adapter.Fill($dataSet) |
| $connection.Close() |
| |
| |
| $dataSet.Tables | Select-Object -Expand RowsCOPY |