问题
I'm trying to find an employee in Active Directory using the following c# code:
"Select userPrincipalName, ADsPath, Department, Mail,
HomeMDB, cn, ssn FROM
'LDAP://" + DomainName + "'
WHERE objectCategory = 'person' and
sAMAccountName = '" + UserName.Replace("'", "''") + "'";
When I run this for an employee with a single quote in the last name (such as "O'Connor") I get the following error:
AdsDsoObject' failed with no error message available, result code: DB_E_NOTABLE(0x80040E37).
I also tried Replace("'", "\''"), nothing is working.
What am I doing wrong? need help.
Thank You!
回答1:
Do the replace on it's own line.
Username.Replace("'", "\'");
"Select userPrincipalName, ADsPath, Department, Mail, HomeMDB, cn, ssn FROM
'LDAP://" + DomainName + "' WHERE objectCategory = 'person' and
sAMAccountName = '" + UserName + "'";
回答2:
You tried:
Replace("'", "\''")
And not:
Replace("'", "\'")
(There's an extra single quote in there).
来源:https://stackoverflow.com/questions/497461/ldap-query-failure-due-to-single-quote