我有一个带有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);
此致 克里斯
答案 0 :(得分:2)
我能做的最好,你动态创建的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)));
答案 1 :(得分:1)
不确定 - 但是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将不必自动解释您的内容,并且可能会出错...