【发布时间】:2011-05-30 09:39:02
【问题描述】:
我有一个将唯一标识符作为参数的存储过程。
它应该是这样工作的(我没有写这个):
SqlCommand command = new SqlCommand("[CheckActivation]", Conn)
{
CommandType = CommandType.StoredProcedure
};
command.Parameters.AddWithValue("@key", key);
return (bool)command.ExecuteScalar();
其中 key 是一个字符串,但它不是。我总是收到“指定的演员表无效”异常。
所以我改写成:
Guid guid = new Guid(key);
using (var command = Conn.CreateCommand())
{
command.CommandText = "[CheckActivation]";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@key", SqlDbType.UniqueIdentifier);
command.Parameters["@key"].Value = guid;
return (bool)command.ExecuteScalar();
}
guid 是一个具有正确值的 Guid 对象,但我仍然遇到同样的异常。
这里出了什么问题?
解决方案:问题出在 return 语句中。 sp 返回一个不能转换为 bool 的 int 值:
return ((int)command.ExecuteScalar() == 1);
【问题讨论】:
-
为什么你的代码中有@key 和@activationkey?你能展示一下 sp 吗?
-
您是否检查过 guid 是否从您的密钥字符串中正确实例化?键不为空吗?
-
而不是将 SqlDbType.UniqueIdentifier 作为字符串并检查,我认为它应该可以工作
-
请添加您的存储过程代码,它将提供更多信息
标签: c# sql-server uniqueidentifier