Powershell访问MySQL

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
[void][system.Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\MySQL\MySQL Connector Net 6.5.4\Assemblies\v4.0\MySql.Data.dll")
$Server="127.0.0.1"
$Database="test"
$user="admin"
$Password="xxx"
$charset="utf8"
$connectionString = "server=$Server;uid=$user;pwd=$Password;database=$Database;charset=$charset"
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$connection.ConnectionString = $connectionString
$sql= "SELECT * FROM product_details"
$command = New-Object MySql.Data.MySqlClient.MySqlCommand($sql, $connection)
$dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
$ds = New-Object System.Data.DataSet
$recordCount = $dataAdapter.Fill($ds)
Write-Host '共有' $recordCount '条记录'


#查看获取的数据
Write-Host '$ds.Tables'
$ds.Tables


#轮询列名
Write-Host '列名如下:'
foreach($table in $ds.Tables)
{
	foreach($column in $table.Columns)
	{
		$column.ColumnName
	}
}

#轮询所有数据
Write-Host '轮询数据:'
foreach($table in $ds.Tables)
{
	foreach($row in $table.Rows)
	{
		$curRow = ''
		foreach ($column in $table.Columns)
		{
			$curRow += [string]$row[$column] + ' '
		}
		$curRow
	}
}

Write-Host '数组方式访问:'
#以数组的方式获取列名 
$ds.Tables[0].columns[0].columnname
#以数组的方式索引数据
$ds.Tables[0].rows[0][1]