System.Data.SQLCommand 中的 Unicode 会发生什么情况

发布于 2024-08-16 06:03:46 字数 965 浏览 2 评论 0原文

我有一个 SQLCommand :

“更新客户集名称 = @name 其中代码 = @code"

和此代码:

cmd.Parameters[0].Value = "بهروز";//(some Unicode characters)  
cmd.Parameters[1].Value = 1;  
cmd.ExecuteNonQuery();  

或此代码:

        UpdateCommand.CommandText = "UPDATE [Customers] SET [Name] = @p1 WHERE (([Code] = @p2) AND ((@p3 = 1 AND [Name] IS NULL) OR ([Name] = @p4)))";
        UpdateCommand.Parameters.Add("@p1", System.Data.SqlDbType.SmallInt, 0, "Name");
        UpdateCommand.Parameters.Add("@p2", System.Data.SqlDbType.NVarChar, 0, "Code");
        UpdateCommand.Parameters.Add("@p3", System.Data.SqlDbType.NText, 0, "Name");
        UpdateCommand.Parameters.Add("@p4", System.Data.SqlDbType.SmallInt, 0, "Name");

当我选择客户表时,我有很多“?”。
为什么 SQLCommand 在使用 SQLDataAdapter 时工作正常?
如何将 Unicode 数据转换为 ANSI?

编辑:
换句话说:
SQLDataAdapter 使用什么代码?
有人有.net框架那部分的源代码吗?

I have a SQLCommand :

"Update Customers Set Name = @name
where code = @code"

and this code:

cmd.Parameters[0].Value = "بهروز";//(some Unicode characters)  
cmd.Parameters[1].Value = 1;  
cmd.ExecuteNonQuery();  

or this code:

        UpdateCommand.CommandText = "UPDATE [Customers] SET [Name] = @p1 WHERE (([Code] = @p2) AND ((@p3 = 1 AND [Name] IS NULL) OR ([Name] = @p4)))";
        UpdateCommand.Parameters.Add("@p1", System.Data.SqlDbType.SmallInt, 0, "Name");
        UpdateCommand.Parameters.Add("@p2", System.Data.SqlDbType.NVarChar, 0, "Code");
        UpdateCommand.Parameters.Add("@p3", System.Data.SqlDbType.NText, 0, "Name");
        UpdateCommand.Parameters.Add("@p4", System.Data.SqlDbType.SmallInt, 0, "Name");

and when I Select Customers Table I have lots of "?"s.
why does SQLCommand work Correct when working with a SQLDataAdapter?
How can i convert my Unicode Data to ANSI?

edit:
in other words :
what code does SQLDataAdapter use?
anyone has the source code of that part of .net framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生一个溪 2024-08-23 06:03:46

亲爱的 Behrooz,只需使用一个简单的 SQL 命令

N'?????????

每当您使用 N 时,您就会强制 SQL 使用 unicode,并且不会出现任何错误。

Dear Behrooz just use a simple SQL command

N'ناصر حاجلو'

whenever you use N you forcesql to use unicode and nothing will corrept.

假装不在乎 2024-08-23 06:03:46

.NET 中的所有字符串都是 Unicode。 .NET 中不存在 ANSI 字符串这样的东西。如果您希望将字符串编码为 ANSI 字节数组,请使用 编码.GetBytes

您的问题可能与将数据发送到存储过程的方式有关。我认为您需要将 sql 数据类型添加到参数中。请尝试以下代码:

cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 30);

有关详细信息,请参阅 SqlParameterCollection.Add 方法。

All strings in .NET are Unicode. There is no such thing as an ANSI string within .NET. If you want a string encoded into a byte array as ANSI, use Encoding.GetBytes.

Your issue may be with the way its sending data to the stored procedure. I think you need to add the sql datatype to the parameter. Try the following code:

cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 30);

See the SqlParameterCollection.Add Method for more information.

梨涡少年 2024-08-23 06:03:46

猜测:尝试显式设置参数的 SqlDbType

cmd.Parameters[0].Value = "بهروز";//(some Unicode characters)  
cmd.Parameters[0].SqlDbType = SqlDbType.NVarChar;
cmd.Parameters[1].Value = 1;  
cmd.ExecuteNonQuery();  

At a guess: try setting the SqlDbType of the parameter explicitly:

cmd.Parameters[0].Value = "بهروز";//(some Unicode characters)  
cmd.Parameters[0].SqlDbType = SqlDbType.NVarChar;
cmd.Parameters[1].Value = 1;  
cmd.ExecuteNonQuery();  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文