简体   繁体   English

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

[英]How to insert strings into a table with their UTF-8 encodings?

I am trying to upload some string values into an Oracle table by means of powershell. 我正在尝试通过powershell将一些字符串值上传到Oracle表中。 However when I upload strings directly some characters are shown up like ? 但是,当我直接上传字符串时,会显示某些字符,例如? in the table. 在桌子上。

Actually, I first parse a text and retrieve some results through regex as below: 实际上,我首先解析文本并通过正则表达式检索一些结果,如下所示:

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

Then I wanna upload this $city variable into a table as below: 然后,我想将此$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()

When I apply this method, the city named Elâzığ appears as Elaz?? 当我应用此方法时,名为Elâzığ的城市将显示为Elaz?? in the table cell. 在表格单元格中。

I guess I have to convert string into UTF-8 but I could not find a solution through web. 我想我必须将字符串转换为UTF-8,但无法通过Web找到解决方案。

Thanks in advance... 提前致谢...

Try this, it should work: 试试这个,它应该可以工作:

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

So your code becomes 所以你的代码变成

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM