如何使用UTF-8编码将字符串插入表中?

时间:2014-02-27 15:18:22

标签: powershell encoding utf-8

我试图通过powershell将一些字符串值上传到Oracle表中。但是,当我直接上传字符串时,某些字符会在表格中显示为?

实际上,我首先通过正则表达式解析文本并检索一些结果,如下所示:

if($wiki_link -match "http:\/\/en\.wikipedia\.org\/wiki\/(.*)") {$city = $matches[1]}

然后我想将此$city变量上传到表格中,如下所示:

[System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient")
$connectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(Host=xxxxxxxxx)(Port=1521)))(CONNECT_DATA=(SERVER = DEDICATED) (SERVICE_NAME =xxxxx)));user id=xxxxxx;password=xxxxx"
$connection = New-Object System.Data.OracleClient.OracleConnection($connectionString)
$connection.Open()
$cmd2=$connection.CreateCommand()
$cmd2.CommandText="insert into mehmet.goo_region (city) values ('$city')"
$rdr2=$cmd2.ExecuteNonQuery()

当我应用此方法时,名为Elâzığ的城市在表格单元格中显示为Elaz??

我想我必须将字符串转换为UTF-8,但我无法通过网络找到解决方案。

提前致谢...

1 个答案:

答案 0 :(得分:0)

试试这个,它应该有效:

$u = New-Object System.Text.UTF8Encoding
$s = $u.GetBytes("YourStringGoesHere")
$u.GetString($s)    ## this is your UTF-8 string

所以你的代码变成了

$u = New-Object System.Text.UTF8Encoding
$s = $u.GetBytes($city)
$utf8city = $u.GetString($s)