[英]Failed to convert parameter value from a Int64 to a Byte[]
I am getting the error: 我收到错误消息:
Failed to convert parameter value from a Int64 to a Byte[].
无法将参数值从Int64转换为Byte []。
Here is what I am doing... 这是我在做什么...
I have a table in my database with the column: 我的数据库中有一个带有列的表:
sessionid | binary(32) | null
When I assign to it, I do this: 当我分配给它时,我这样做:
user.LastActivityDate = DateTime.Now;
user.SessionId = user.LastActivityDate.ToBinary();
SessionId is an inherited property from an interface: SessionId是从接口继承的属性:
long? SessionId { get; set; }
And here it is being accessed in my User class: 在我的User类中可以访问它:
[SettingsAllowAnonymous(false), CustomProviderData("SessionId;string")]
public long? SessionId { get { return base["SessionId"] as long?; } set { base["SessionId"] = value; } }
In my custom profile provider, the following command assigns the value: 在我的自定义配置文件提供程序中,以下命令分配值:
sqlCommand.Parameters.Add("sessionid", SqlDbType.Binary, 32).Value = settingsPropertyValue.PropertyValue;
When the "sqlCommand.ExecuteNonQuery()" command executes an update stored procedure is executed with the parameter: 当“ sqlCommand.ExecuteNonQuery()”命令执行更新存储过程时,将使用以下参数执行:
@sessionid binary(32)
And the result is the error: 结果是错误:
"Failed to convert parameter value from a Int64 to a Byte[]."
“无法将参数值从Int64转换为Byte []。
I have tried: 我努力了:
Any ideas? 有任何想法吗? Thanks.
谢谢。
SqlDbType.Binary
is not correct (when you add the parameter) -- that's for extended binary data of arbitrary length. SqlDbType.Binary
是不正确的(添加参数时)-用于任意长度的扩展二进制数据。 You want to just represent it as a 64-bit integer, which is SqlDbType.BigInt
. 您只想将其表示为64位整数,即
SqlDbType.BigInt
。
By the way, why are you doing this in the first place? 顺便说一句,为什么首先要这样做? SQL has a date-time type;
SQL具有日期时间类型; you can just store it like that.
您可以像这样存储它。
(You could go about it in the opposite, perverse way and instead convert the long to an eight-byte array and pass that, but there's no conceivable reason you would want to store it in that manner.) (您可以以相反的方式处理它,而是将long转换为一个八字节的数组并将其传递,但是没有可以想到的原因想要以这种方式存储它。)
ToBinary
doesn't do what you think it does. ToBinary
不执行您认为的操作。
The DateTime.ToBinary
method returns a long
value, not a byte array. DateTime.ToBinary
方法返回一个long
值,而不是字节数组。
What are you trying to do? 你想做什么?
If you're trying to store the datetime, you should just use a DataTime column. 如果您要存储日期时间,则应仅使用DataTime列。
If you actually want a byte array, please provide more details. 如果您确实需要字节数组,请提供更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.