标题: [转载代码] PowerShell访问数据库 [打印本页]
作者: DAIC 时间: 2013-9-10 21:42 标题: PowerShell访问数据库
访问mssql- $server="192.168.8.99"
- $Database="test"
- $user="admin"
- $Password="ppp123"
- $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
- $CnnString ="Server = $server; Database = $Database;User Id = $user; Password = $Password"
- $SqlConnection.ConnectionString = $CnnString
- $CC = $SqlConnection.CreateCommand();
- $cc.CommandText="select * from tables "
- $da=New-Object System.Data.SqlClient.SqlDataAdapter($cc)
- $ds=New-Object System.Data.DataSet
- $da.fill([system.data.dataset]$ds)
- #查看获取的数据
- $ds.Tables
- #轮询列名
- foreach($table in $ds.Tables){
- foreach($column in $table.Columns){
- $column.ColumnName
- }
- }
- #轮询所有数据
- foreach($table in $ds.Tables){
- foreach($row in $table.Rows){
- foreach ($column in $table.Columns){
- $row[$column]
- }
- }
- }
- #以数组的方式获取列名
- $ds.Tables[0].columns[0].columnname
- #以数组的方式索引数据
- $ds.Tables[0].rows[0][1]
复制代码
访问mysql- #[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
- #[void][system.reflection.Assembly]::LoadFrom("C:\Program Files\MySQL\MySQL Connector Net 6.2.4\Assemblies\MySql.Data.dll")
- [void][system.reflection.Assembly]::LoadFrom("C:\Program Files\MySQL\MySQL Connector Net 5.0.9\Binaries\.NET 2.0\MySql.Data.dll")
- $server="192.168.8.99"
- $Database="test"
- $user="admin"
- $Password="ppp123"
- $charset="gb2312"
- $connectionString = "server=$server;uid=$user;pwd=$Password;database=$Database;charset=$charset"
- $connection = New-Object MySql.Data.MySqlClient.MySqlConnection
- $connection.ConnectionString = $connectionString
- $sql= "SELECT * FROM aaa"
- $command = New-Object MySql.Data.MySqlClient.MySqlCommand($sql, $connection)
- $dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
- $dataSet = New-Object System.Data.DataSet
- $recordCount = $dataAdapter.Fill($dataSet)
- #[System.Data.DataSet]的操作同mssql
复制代码
http://hi.baidu.com/shrekzz/item/e75f2b0bbffe9413ebfe3859
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |