无法通过 SQL 参数将 C# 中的字符串转换为 uniqueidentifier
我有一个表,其中包含 uniqueidentifier 类型的 PK 列。
我正在尝试从 C# 编写一条更新语句,其中标识符为字符串。
这是我正在尝试的代码:
string sql_start = "update SecurityUserGroup set ";
int paramPos = 0;
var paramList = new List<SqlParameter>();
if (roleName != null)
{
sql_start += " RoleName = '{" + paramPos + "}'";
paramList.Add(new SqlParameter(""+paramPos, roleName));
paramPos++;
}
if (activeDirectoryGroup != null)
{
sql_start += " ActiveDirectoryGroup = '{" + paramPos + "}'";
paramList.Add(new SqlParameter(""+paramPos, activeDirectoryGroup));
paramPos++;
}
//sql_start += " where SecurityUserGroupId = cast('{" + paramPos + "}' as uniqueidentifier)";
//sql_start += " where SecurityUserGroupId = convert(uniqueidentifier, '{" + paramPos + "}')";
sql_start += @" where SecurityUserGroupId = '{" + paramPos + @"}'";
paramList.Add(new SqlParameter(""+paramPos, new Guid(id)));
//paramList.Add(new SqlParameter(""+paramPos, id));
SqlHelper.ExecSql(sql_start, paramList);
SqlHelper.ExecSql 函数执行此操作:
public static void ExecSql(string sql, List<SqlParameter> parms)
{
using (SqlConnection con = GetConnection())
{
SqlCommand command = new SqlCommand(sql, con) {CommandType = CommandType.Text};
foreach (SqlParameter parm in parms)
{
command.Parameters.Add(parm);
}
command.ExecuteNonQuery();
}
}
但我要么得到无效语法,要么“从字符串转换为 uniqueidentifier 时转换失败”
我之前在提取时遇到此问题,最终将转换放入一个存储过程,看来我也需要将其变成一个存储过程。
预先感谢您对此的任何想法/提示。
编辑:示例 ID 看起来像这样“45d9ec51-1e52-49d8-9139-51f18fe13563”,它看起来像我通过 Management Studio 在表上看到的内容。
EDIT2:这是最终有效的代码(使用@而不是{}):
var sql_start = new StringBuilder("update SecurityUserGroup set ");
int paramPos = 0;
var paramList = new List<SqlParameter>();
if (roleName != null)
{
sql_start.Append(" RoleName = @" + paramPos);
paramList.Add(new SqlParameter("@"+paramPos, roleName));
paramPos++;
}
if (activeDirectoryGroup != null)
{
if (paramPos > 0) sql_start.Append(",");
sql_start.Append(" ActiveDirectoryGroup = @" + paramPos);
paramList.Add(new SqlParameter("@"+paramPos, activeDirectoryGroup));
paramPos++;
}
sql_start.Append(" where SecurityUserGroupId = @id");
paramList.Add(new SqlParameter("@id", id));
SqlHelper.ExecSql(sql_start.ToString(), paramList);
问候, 克里斯
I have a table with a PK column of type uniqueidentifier.
I am trying to write an update statement from C#, where I have the identifier as a string.
Here is the code I am trying:
string sql_start = "update SecurityUserGroup set ";
int paramPos = 0;
var paramList = new List<SqlParameter>();
if (roleName != null)
{
sql_start += " RoleName = '{" + paramPos + "}'";
paramList.Add(new SqlParameter(""+paramPos, roleName));
paramPos++;
}
if (activeDirectoryGroup != null)
{
sql_start += " ActiveDirectoryGroup = '{" + paramPos + "}'";
paramList.Add(new SqlParameter(""+paramPos, activeDirectoryGroup));
paramPos++;
}
//sql_start += " where SecurityUserGroupId = cast('{" + paramPos + "}' as uniqueidentifier)";
//sql_start += " where SecurityUserGroupId = convert(uniqueidentifier, '{" + paramPos + "}')";
sql_start += @" where SecurityUserGroupId = '{" + paramPos + @"}'";
paramList.Add(new SqlParameter(""+paramPos, new Guid(id)));
//paramList.Add(new SqlParameter(""+paramPos, id));
SqlHelper.ExecSql(sql_start, paramList);
The SqlHelper.ExecSql function does this:
public static void ExecSql(string sql, List<SqlParameter> parms)
{
using (SqlConnection con = GetConnection())
{
SqlCommand command = new SqlCommand(sql, con) {CommandType = CommandType.Text};
foreach (SqlParameter parm in parms)
{
command.Parameters.Add(parm);
}
command.ExecuteNonQuery();
}
}
But I either get invalid syntax or "Conversion failed when converting from a character string to uniqueidentifier"
I previously had this problem on the extraction and ended up putting the conversion into a stored procedure and it seems I will need to make this into a stored procedure too.
Thanks in advance for any thoughts/tips on this.
EDIT: A sample id looks like this "45d9ec51-1e52-49d8-9139-51f18fe13563", which looks like what I see on the table via Management Studio.
EDIT2: Here is the code that worked in the end (using @ instead of {}):
var sql_start = new StringBuilder("update SecurityUserGroup set ");
int paramPos = 0;
var paramList = new List<SqlParameter>();
if (roleName != null)
{
sql_start.Append(" RoleName = @" + paramPos);
paramList.Add(new SqlParameter("@"+paramPos, roleName));
paramPos++;
}
if (activeDirectoryGroup != null)
{
if (paramPos > 0) sql_start.Append(",");
sql_start.Append(" ActiveDirectoryGroup = @" + paramPos);
paramList.Add(new SqlParameter("@"+paramPos, activeDirectoryGroup));
paramPos++;
}
sql_start.Append(" where SecurityUserGroupId = @id");
paramList.Add(new SqlParameter("@id", id));
SqlHelper.ExecSql(sql_start.ToString(), paramList);
Regards,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,动态创建的 SQL 最终会看起来像这样:
当您可能需要的是这样的东西时:
为此,请将 : 之类的代码替换为 :
之类的代码:
Best I could make out, your dynamically created SQL is going to end up looking something like :
When what you probably need is something like :
To do that, replace code like :
with code like :
不确定 - 但也许 SQL Server 将您的参数值误解为字符串,而不是 GUID,在这里:
我将使用以下方法添加此内容:
如果您使用显式的特定类型创建
SqlParameter
,SQL Server 会获胜不必自动解释你的东西,也许会弄错......Not sure - but maybe SQL Server misinterprets your parameter value as a string, instead of a GUID, here:
I would add this using:
If you create the
SqlParameter
with an explicit, specific type, SQL Server won't have to auto-interpret your stuff and maybe get it wrong...