无法通过 SQL 参数将 C# 中的字符串转换为 uniqueidentifier

发布于 2024-09-07 16:22:51 字数 2783 浏览 4 评论 0原文

我有一个表,其中包含 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 技术交流群。

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

发布评论

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

评论(2

忘东忘西忘不掉你 2024-09-14 16:22:51

据我所知,动态创建的 SQL 最终会看起来像这样:

update SecurityUserGroup set RoleName = '{0}' ActiveDirectoryGroup = '{1}' where SecurityUserGroupId = '{2}'

当您可能需要的是这样的东西时:

update SecurityUserGroup set RoleName = @RoleName, ActiveDirectoryGroup = @ActiveDirectoryGroup where SecurityUserGroupId = @SecurityUserGroupId

为此,请将 : 之类的代码替换为 :

sql_start += @" where SecurityUserGroupId = '{" + paramPos + @"}'";
paramList.Add(new SqlParameter(""+paramPos, new Guid(id)));

之类的代码:

sql_start += " where SecurityUserGroupId = @SecurityUserGroupId";
paramList.Add(new SqlParameter("@SecurityUserGroupId", new Guid(id)));

Best I could make out, your dynamically created SQL is going to end up looking something like :

update SecurityUserGroup set RoleName = '{0}' ActiveDirectoryGroup = '{1}' where SecurityUserGroupId = '{2}'

When what you probably need is something like :

update SecurityUserGroup set RoleName = @RoleName, ActiveDirectoryGroup = @ActiveDirectoryGroup where SecurityUserGroupId = @SecurityUserGroupId

To do that, replace code like :

sql_start += @" where SecurityUserGroupId = '{" + paramPos + @"}'";
paramList.Add(new SqlParameter(""+paramPos, new Guid(id)));

with code like :

sql_start += " where SecurityUserGroupId = @SecurityUserGroupId";
paramList.Add(new SqlParameter("@SecurityUserGroupId", new Guid(id)));
奢欲 2024-09-14 16:22:51

不确定 - 但也许 SQL Server 将您的参数值误解为字符串,而不是 GUID,在这里:

paramList.Add(new SqlParameter(""+paramPos, new Guid(id)));

我将使用以下方法添加此内容:

SqlParameter workParam = new SqlParameter("" + paramPos, SqlDbType.Uniqueidentifier);
workParam.Value = new Guid(id);
paramList.Add(workParam);

如果您使用显式的特定类型创建 SqlParameter,SQL Server 会获胜不必自动解释你的东西,也许会弄错......

Not sure - but maybe SQL Server misinterprets your parameter value as a string, instead of a GUID, here:

paramList.Add(new SqlParameter(""+paramPos, new Guid(id)));

I would add this using:

SqlParameter workParam = new SqlParameter("" + paramPos, SqlDbType.Uniqueidentifier);
workParam.Value = new Guid(id);
paramList.Add(workParam);

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文