1、首先需要安装插件  下载地址如下 ​​mysql-connector-net​

2、下载安装后,需要找到其安装的目录,并找到用于连接和操作MySQL的DLL文件;需要注意自己电脑的.net版本选择相应的dll文件

3、在powershell执行以下语句,大致意思是加载这个dll程式

$mysqlnet="C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.30\Assemblies\v4.5.2\MySql.Data.dll"
[void][system.Reflection.Assembly]::LoadFrom($mysqlnet) | Out-Null

4、连接mysql

$Server="127.0.0.1"
$Database="sql_"
$user="root"
$Password= ""
$charset="utf8"
$connectionString = "server=$Server;uid=$user;pwd=$Password;database=$Database;charset=$charset"
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection($connectionString)
$connection.Open()

5、执行sql语句  

$insertcommand = New-Object MySql.Data.MySqlClient.MySqlCommand  我的理解是创建一个客户端连接对象

$insertsql = "INSERT INTO tablename (name,age) VALUES('jiaxiaojian','29');"
$insertcommand = New-Object MySql.Data.MySqlClient.MySqlCommand
$insertcommand.Connection=$connection
$insertcommand.CommandText=$insertsql
$insertcommand.ExecuteNonQuery()

6、关闭连接

$connection.Close()