首页 新闻 搜索 专区 学院

Ado.Net插入中文数据乱码问题

0
[已关闭问题]
这样子写,执行以后数据库中中文字符显示乱码,PS:<span style="color: rgb(128, 0, 0);">Article_Subject 为NCHAR,</span><span style="color: rgb(128, 0, 0);">Article_Content 为Ntext</span><span style="color: rgb(128, 0, 0);"> </span><div class="cnblogs_code"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><img src="/Images/OutliningIndicators/None.gif" align="top"><span style="color: rgb(0, 0, 0);">strSql&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">INSERT&nbsp;INTO&nbsp;Articles&nbsp;([Article_Subject]&nbsp;,[Article_Content]&nbsp;,[Author_ID]&nbsp;,[Post_Time])&nbsp;VALUES&nbsp;('测试','测试','FrancisLiu','</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">DateTime.Today.ToShortDateString()</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">')</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;SqlCommand&nbsp;cmd&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;SqlCommand();<br><img src="/Images/OutliningIndicators/None.gif" align="top">cmd.Connection&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;conn;<br><img src="/Images/OutliningIndicators/None.gif" align="top">cmd.CommandText&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;strSql;<br><img src="/Images/OutliningIndicators/None.gif" align="top">cmd.ExecuteNonQuery();<br>用 SqlParameter没问题的<br></span><div class="cnblogs_code"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><img src="/Images/OutliningIndicators/None.gif" align="top"><span style="color: rgb(0, 0, 0);">strSql.Append(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">insert&nbsp;into&nbsp;[Articles](</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;strSql.Append(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">[article_Subject],[article_Content],[author_id],[post_time])</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strSql.Append(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&nbsp;values&nbsp;(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strSql.Append(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">@article_Subject,@article_Content,@author_id,@make_time,@sReply_count)</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br><img src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_304_634_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_304_634_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_304_634_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_304_634_Closed_Text').style.display='inline';" align="top"><img src="/Images/OutliningIndicators/ContractedBlock.gif" id="Codehighlighter1_304_634_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_304_634_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_304_634_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_304_634_Open_Text').style.display='inline';" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SqlParameter[]&nbsp;parameters&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span id="Codehighlighter1_304_634_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;"><img src="/images/dot.gif"></span><span id="Codehighlighter1_304_634_Open_Text"><span style="color: rgb(0, 0, 0);">{<br><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;SqlParameter(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">@article_Subject</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;SqlDbType.NChar,</span><span style="color: rgb(128, 0, 128);">40</span><span style="color: rgb(0, 0, 0);">),<br><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;SqlParameter(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">@article_Content</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;SqlDbType.NText),<br><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;SqlParameter(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">@author_id</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;SqlDbType.NChar,</span><span style="color: rgb(128, 0, 128);">10</span><span style="color: rgb(0, 0, 0);">),<br><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;SqlParameter(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">@make_time</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;SqlDbType.NChar,</span><span style="color: rgb(128, 0, 128);">25</span><span style="color: rgb(0, 0, 0);">)<br><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><img src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);">;<br><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameters[</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">].Value&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;model.article_Subject;<br><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameters[</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">].Value&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;model.article_Content;<br><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameters[</span><span style="color: rgb(128, 0, 128);">2</span><span style="color: rgb(0, 0, 0);">].Value&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;model.author_id;<br><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameters[</span><span style="color: rgb(128, 0, 128);">3</span><span style="color: rgb(0, 0, 0);">].Value&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;model.make_time;&nbsp;&nbsp;&nbsp; <br></span></div><br><span style="color: rgb(0, 0, 0);"><br></span></div>用Linq to sql也是没问题的<br><br>
FrancisLiu的主页 FrancisLiu | 初学一级 | 园豆:200
提问于:2008-07-23 16:59
< >
分享
其他回答(3)
0
这个问题很怪呀,直接用strSql 在数据库的查询分析器中执行会怎么样呢?
玉开 | 园豆:8822 (大侠五级) | 2008-07-23 17:25
0
两个字段都是乱码吗? 你可以调试一下程序,在cmd.ExecuteNonQuery();这句上设置断点然后分别查看cmd.CommandText和strSql的值是什么,看是否有乱码存在,也就是在把命令发送到数据库前检查写入的数据是否有乱码。 如果没有,那么启动sql profile(事件探查器)看实际执行的语句是什么,是不是在从程序到数据库这个过程中出了问题。 怀疑你的程序编码方式和数据库编码不一致才造成这个问题
丁学 | 园豆:18530 (专家六级) | 2008-07-24 15:29
0
试试在字符串前加个N行不行.. strSql = "INSERT INTO Articles ([Article_Subject] ,[Article_Content] ,[Author_ID] ,[Post_Time]) VALUES (N'测试',N'测试','FrancisLiu','"+DateTime.Today.ToShortDateString()+"')";
deerchao | 园豆:8167 (大侠五级) | 2008-07-24 21:32
0
是单引号的问题吧,试试 strSql = @"INSERT INTO Articles ([Article_Subject] ,[Article_Content] ,[Author_ID] ,[Post_Time]) VALUES ('测试','测试','FrancisLiu','" +…… 或者把 ' 变成 \'
刘勇飞 | 园豆:335 (菜鸟二级) | 2008-07-30 20:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册