sql2005 xml 数据中文字符乱码的问题,请教高人!在线等

yygyogfny 2009-05-13 01:59:14

declare @FromPalletXML xml
set @FromPalletXML='
<NewDataSet>
<Table>
<whpi_iTagID>1</whpi_iTagID>
<whpi_cTagNo>BB1983</whpi_cTagNo>
<whpi_cOperationStatusCD>P10</whpi_cOperationStatusCD>
<whpi_cOperationStatusDesc>卡板已經做過入板操作</whpi_cOperationStatusDesc>
<whpi_lGoodsFICTake>1</whpi_lGoodsFICTake>
<whpi_cFICstatusDesc>已抽貨</whpi_cFICstatusDesc>
<whpi_lGoodsFICResult>1</whpi_lGoodsFICResult>
<whpi_cFICResultDesc>合格</whpi_cFICResultDesc>
<Whpi_lAutoLoading>0</Whpi_lAutoLoading>
<Whpi_cGroupNo />
<Whpi_fPalletLength>120</Whpi_fPalletLength>
<Whpi_fPalletWeight>120</Whpi_fPalletWeight>
<Whpi_fPalletHeight>120</Whpi_fPalletHeight>
</Table>
</NewDataSet>'

declare @iDoc1 int
exec sp_xml_preparedocument @iDoc1 output,@FromPalletXML
select * --into #FromPallet
from
openxml(@iDoc1,'/NewDataSet/Table',2)
with
(
whpi_iTagID int ,
whpi_cTagNo varchar(24),
whpi_cOperationStatusCD varchar(3),
whpi_cOperationStatusDesc nvarchar(20),
whpi_lGoodsFICTake bit,
whpi_cFICstatusDesc nvarchar(10),
whpi_lGoodsFICResult bit,
whpi_cFICResultDesc nvarchar(10),
Whpi_lAutoLoading bit,
Whpi_cGroupNo varchar(20),
Whpi_fPalletLength decimal(18),
Whpi_fPalletWeight decimal(18),
Whpi_fPalletHeight decimal(18)
) As a
EXEC sp_xml_removedocument @iDoc1


whpi_iTagID whpi_cTagNo whpi_cOperationStatusCD whpi_cOperationStatusDesc whpi_lGoodsFICTake whpi_cFICstatusDesc whpi_lGoodsFICResult whpi_cFICResultDesc Whpi_lAutoLoading Whpi_cGroupNo Whpi_fPalletLength Whpi_fPalletWeight Whpi_fPalletHeight
----------- ------------------------ ----------------------- ------------------------- ------------------ ------------------- -------------------- ------------------- ----------------- -------------------- --------------------------------------- --------------------------------------- ---------------------------------------
1 BB1983 P10 ?????????? 1 ??? 1 ?? 0 120 120 120

(1 row(s) affected)



...全文
507 52 打赏 收藏 转发到动态 举报
写回复
用AI写文章
52 条回复
切换为时间正序
请发表友善的回复…
发表回复
ai_li7758521 2009-05-13
  • 打赏
  • 举报
回复
最简单的加个N:
@FromPalletXML='
<NewDataSet>
<Table>
....'
数据库字符集问题


nzperfect 2009-05-13
  • 打赏
  • 举报
回复
在区域选项里安装中文字符集再试试.
fcuandy 2009-05-13
  • 打赏
  • 举报
回复
你的测试方式不对。

我上面已经跟你说了。

在c#里参数声明为nvarchar类型,赋值时,就为以unicode方式编码, 等于SQL中给NVARCHAR赋值时前辍加N
而sql中只是把存储着unicode的变量值又传递给另一个。

比如存储过程中

set @PalletXML=@n -- @n是存储过程输入参数,在c#中被声明为nvarchar.
这样@palletXML值就会是unicode编码, 不必要写N前辍。 把字串赋给n系字串变量时前辍加N 只是表示把字串以unicode方式编码。 至于这个,在c#中声明sqldbtype.nvarchar已在这一步做了。

你想写成 set @PalletXML=N@n 还报错呢,不需要这样。
yygyogfny 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 47 楼 fcuandy 的回复:]
那就可以了,但要注意,存储过程参数要声明为nvarchar,或xml

比如
create proc p
(@n nvarchar(max))
as
begin
declare @x xml
set @x = @n
. ...
end


cmd.commandtype=commmandtype.storedproc;
cmd.commandtext='p';
cmd.parameters.add('@n',sqldbtype.nvarchar,........
[/Quote]


先暂时不考虑客户端传的参数,
我现在就是在查询分析器里做试验.

之所以引入传入参数是因为"没办法在传入的参数前加一个N字符"
yygyogfny 2009-05-13
  • 打赏
  • 举报
回复


declare @PalletXML nvarchar(max)



--卡板資料XML
set @PalletXML='
<NewDataSet>
<Table>
<whpi_iTagID>1</whpi_iTagID>
<whpi_cTagNo>BB1983</whpi_cTagNo>
<whpi_cOperationStatusCD>P10</whpi_cOperationStatusCD>
<whpi_cOperationStatusDesc>卡板已經做過入板操作</whpi_cOperationStatusDesc>
<whpi_cGoodsFICTake>1</whpi_cGoodsFICTake>
<whpi_cFICstatusDesc>合格</whpi_cFICstatusDesc>
<Whpi_lAutoLoading>0</Whpi_lAutoLoading>
<Whpi_cGroupNo />
<Whpi_fPalletLength>120</Whpi_fPalletLength>
<Whpi_fPalletWeight>120</Whpi_fPalletWeight>
<Whpi_fPalletHeight>120</Whpi_fPalletHeight>
</Table>
</NewDataSet>'

select @PalletXML


<NewDataSet>
<Table>
<whpi_iTagID>1</whpi_iTagID>
<whpi_cTagNo>BB1983</whpi_cTagNo>
<whpi_cOperationStatusCD>P10</whpi_cOperationStatusCD>
<whpi_cOperationStatusDesc>??????????</whpi_cOperationStatusDesc>
<whpi_cGoodsFICTake>1

(1 row(s) affected)

fcuandy 2009-05-13
  • 打赏
  • 举报
回复
那就可以了,但要注意,存储过程参数要声明为nvarchar,或xml

比如
create proc p
(@n nvarchar(max))
as
begin
declare @x xml
set @x = @n
. ...
end



cmd.commandtype=commmandtype.storedproc;
cmd.commandtext='p';
cmd.parameters.add('@n',sqldbtype.nvarchar,........
jinjazz 2009-05-13
  • 打赏
  • 举报
回复
那你试试把参数类型改为nvarchar(max)然后在存储过程里面转为xml
yygyogfny 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 43 楼 fcuandy 的回复:]
存储过程输入参数为什么类型?

XML?

调用存储过程时 对参数的声明为什么类型? 用的什么语言?
以c#调用为例,
将类型声明为 dbtype.nvarchar,则赋值时自动会以unicode编码,而勿须加n.
声明为dbtype.varchar,当然依赖排序规则,在latin规则下,自然乱码。
[/Quote]

确实是C#.
但现在暂定输入参数为nvarchar,如何修改?
yygyogfny 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 41 楼 fcuandy 的回复:]
如果是排序规则问题,加N再SELECT应该不会乱码。

如果还有乱码,确认自己在区域选项里是否安装了中文字符集。如果没有乱码,那么后面的也不会有乱码。

如果区域选项设置里也安装了中文字符集,加了N,还有乱码,那只能是RPWT
[/Quote]

加N后没有出现乱码,
那按高人的意思是说是排序规则的问题?

那上面那些代码应该如何修改排序规则?我是直接从xml片断里读取到datatable中的

请指教啊~~

至于RPWT,是不可能存在的,不信你看我结贴率嘛~~
fcuandy 2009-05-13
  • 打赏
  • 举报
回复
存储过程输入参数为什么类型?

XML?

调用存储过程时 对参数的声明为什么类型? 用的什么语言?
以c#调用为例,
将类型声明为 dbtype.nvarchar,则赋值时自动会以unicode编码,而勿须加n.
声明为dbtype.varchar,当然依赖排序规则,在latin规则下,自然乱码。
yygyogfny 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 39 楼 fcuandy 的回复:]
在对
set @FromPalletXml = N' <NewDataSet>
<Table>
<whpi_iTag.....

赋值时,加上N, 然后再select @FromPalletXml 看看是否乱码, 以便确认问题在哪。
[/Quote]

加上N后是没有乱码了,但这个xml片断是从存储过程的传入参数,所以也"不便加N"啊
fcuandy 2009-05-13
  • 打赏
  • 举报
回复
如果是排序规则问题,加N再SELECT应该不会乱码。

如果还有乱码,确认自己在区域选项里是否安装了中文字符集。如果没有乱码,那么后面的也不会有乱码。

如果区域选项设置里也安装了中文字符集,加了N,还有乱码,那只能是RPWT
jinjazz 2009-05-13
  • 打赏
  • 举报
回复
因为他是e文数据库,估计和数据字符集以及排序规则有关
fcuandy 2009-05-13
  • 打赏
  • 举报
回复
在对
set @FromPalletXml = N'<NewDataSet>
<Table>
<whpi_iTag.....

赋值时,加上N, 然后再select @FromPalletXml 看看是否乱码, 以便确认问题在哪。
yygyogfny 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 37 楼 fcuandy 的回复:]
直接select @FromPalletXML 是否乱码
[/Quote]

感谢关注~~
也是乱码~~~
fcuandy 2009-05-13
  • 打赏
  • 举报
回复
直接select @FromPalletXML 是否乱码
yygyogfny 2009-05-13
  • 打赏
  • 举报
回复
真没别的什么办法了吗????????????????
yygyogfny 2009-05-13
  • 打赏
  • 举报
回复
还在等待~~
meheartfly 2009-05-13
  • 打赏
  • 举报
回复
额不是高人,而是来接分的!
yygyogfny 2009-05-13
  • 打赏
  • 举报
回复
没有其它办法吗?
加载更多回复(32)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧