C# 查询返回错误的值?

C# 查询返回错误的值?,c#,sql,C#,Sql,多亏了zambonee的帮助 我正在使用EF编写一个查询,从aspNet_Users表返回userId。然后我用这个ID删除membership、userINroles和users中的记录……但是查询返回的userId是错误的值……并且我连接到了正确的DB iv检查了连接字符串并测试了其他数据 using (DEntities Context = DAOHelper.GetObjectContext<DEntities>()) { Guid aspUserIdToRemove

多亏了zambonee的帮助

我正在使用EF编写一个查询,从aspNet_Users表返回userId。然后我用这个ID删除membership、userINroles和users中的记录……但是查询返回的userId是错误的值……并且我连接到了正确的DB iv检查了连接字符串并测试了其他数据

    using (DEntities Context = DAOHelper.GetObjectContext<DEntities>())
{
Guid aspUserIdToRemove = Context.ExecuteStoreQuery<string>("Select UserId FROM aspnet_Users where UserName LIKE '%" + userName + "%'").ElementType.GUID;

string aspUserId = aspUserIdToRemove.ToString();
aspUserId = aspUserId.Replace("{", string.Empty);
aspUserId = aspUserId.Replace("}", string.Empty);

Context .ExecuteStoreCommand("DELETE FROM aspnet_Membership where UserId = '" + aspUserId + "'");
Context .ExecuteStoreCommand("DELETE FROM aspnet_UsersInRoles where UserId = '" + aspUserId + "'");
Context .ExecuteStoreCommand("DELETE FROM aspnet_Users where UserId = '" + aspUserId + "'");
使用(DEntities Context=daoheloper.GetObjectContext())
{
Guid aspUserIdToRemove=Context.ExecuteStoreQuery(“从aspnet_用户中选择用户名,其中用户名为“%”+用户名+“%””)。ElementType.Guid;
字符串aspUserId=aspUserIdToRemove.ToString();
aspUserId=aspUserId.Replace(“{”,string.Empty);
aspUserId=aspUserId.Replace(“}”,string.Empty);
ExecuteStoreCommand(“从aspnet_成员身份中删除,其中UserId='“+aspUserId+””);
ExecuteStoreCommand(“从aspnet_UsersInRoles中删除,其中UserId='“+aspUserId+””);
ExecuteStoreCommand(“从aspnet_用户中删除,其中UserId='“+aspUserId+””);
aspUserIdToRemove应返回{31E62355-8AE2-4C44-A270-2F185581B742}时返回{296afbff-1b0b-3ff5-9d6c-4e7e599f8b57}

{296afbff-1b0b-3ff5-9d6c-4e7e599f8b57}甚至不存在于数据库中…有人知道出了什么问题吗?谢谢

只是为了强调我在同一个数据库中,我在不同的表上执行了进一步的删除命令,并确认它们已被删除

下面是评论的内容-

var s = dnnEntitiesDbContext.ExecuteStoreQuery<string>("Select UserId FROM aspnet_Users where UserName LIKE '%" + userName + "%'");
var s=DnnitiesDbContext.ExecuteStoreQuery(“从aspnet_用户中选择用户名,其中用户名类似“%”+用户名+“%”);
s、 elementtype.GUID保存296afbff-1b0b-3ff5-9d6c-4e7e599f8b57

但是s.base.elementType.baseType.Guid返回一个差异Guid“81c5f…”。。。。
但是没有我正在查找的代码的迹象

您可能对
ExecuteStoreQuery
有一些误解,它将返回您指定的
类型
。在您的情况下,它将返回
字符串

Guid aspUserIdToRemove = Context.ExecuteStoreQuery<string>("Select UserId FROM aspnet_Users where UserName LIKE '%" + userName + "%'").ElementType.GUID;
您可能更希望避免SQL注入,并使用参数

string aspUserIdToRemove = Context.ExecuteStoreQuery<string>("Select UserId FROM aspnet_Users where UserName LIKE '%{0}%'", userName);
string aspUserIdToRemove=Context.ExecuteStoreQuery(“从aspnet_用户中选择UserId,其中用户名“{0}%”,UserName);
详细信息您可以查看

由于
aspUserIdToRemove
是一个字符串,您不需要在其上使用
.ToString()
。但是,我没有足够的数据,您可能需要检查是否需要转义“{}”


此外,从您的评论来看,
\
是一个转义字符,如果您想在字符串中转义,您需要使用
\\
()

来转义它,只是为了澄清-语句“从aspnet\u用户中选择UserId,其中UserName类似“%”+UserName+“%”吗从控制台运行时返回正确的GUID?是的,当我从aspnet_用户运行Select UserId时,其中SQL中的用户名“%JimBob%”返回31E62355-8AE2-4C44-A270-2F185581B742您错误地使用了EF-您应该避免使用
ExecuteStoreQuery
而改用强类型基于模型的API,如果您绝对必须使用
ExecuteStoreQuery
然后使用参数化查询!-您的代码容易受到SQL注入攻击。是否可以将GUID保留为字符串值,而不是尝试将其转换为GUID类型(然后将其转换回字符串)?我想知道SQL和C#之间的字符串和GUI之间的转换是否有问题。@Dai我正在处理另一个用户创建的现有代码,我正在尝试修复一个错误。。。
string aspUserIdToRemove = Context.ExecuteStoreQuery<string>("Select UserId FROM aspnet_Users where UserName LIKE '%{0}%'", userName);