首页 > 解决方案 > PowerShell MySQL 插入外来字符失败

问题描述

如果我运行下面的 MySQL 插入,韩文文本就成功插入到表中

INSERT INTO `table` (`site`, `published`, `title`, `link`, `body`, `language`) VALUES ('something.com', '2021-09-02 02:11:01', '안녕하세요 월드입니다!', 'something.com', 'none', 'Korean');

但在powershell中,韩文文本被插入到表格中????? ?????!

function ConnectToDatabase([string]$user, [string]$pass, [string]$MySQLHost, [string]$database) {
    #write-host "--------"
    #write-host "Connecting to database" 
    # Load MySQL .NET Connector Objects 
    [void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data") 
    # Open Connection 
    $connStr = "server=" + $MySQLHost + ";port=3306;uid=" + $user + ";pwd=" + $pass + ";database="+$database+";Pooling=FALSE" 
    try {
        $conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr) 
        $conn.Open()
    } catch [System.Management.Automation.PSArgumentException] {
        write-host "Unable to connect to MySQL server, do you have the MySQL connector installed..?"
        write-host $_
        #Exit
    } catch {
        write-host "Unable to connect to MySQL server..."
        write-host $_.Exception.GetType().FullName
        write-host $_.Exception.Message
        #exit
    }
    #write-host "Connected to MySQL database : $MySQLHost\$database"
    #write-host "--------"
    return $conn 
}
$conn = ConnectToDatabase "user" "pass" "127.0.0.1" "db"
$query='INSERT INTO `table` (`site`, `published`, `title`, `link`, `body`, `language`) VALUES ("something.com", "2021-09-02 02:11:01", "안녕하세요 월드입니다!", "something.com", "none", "Korean");'          
$Command = New-Object MySql.Data.MySqlClient.MySqlCommand $query, $conn
$null = $Command.ExecuteNonQuery()
$conn.close()

标签: mysqlpowershellutf-8

解决方案


推荐阅读