如何通过LDAP检索到的电子邮件地址findExchange Server名称?

我们的组织有一个BES服务器,我已经编写了一个代码来根据他们的交换电子邮件地址find用户,现在我想用C#.net通过他们的交换服务器名称(实例)来重定位它们。 我有一个与我的SQL Server数据库中的服务器名称表,但我不知道我应该如何处理它。 我已经开始了,但是我不知道应该如何继续。 你能帮我找出我的错误在哪里吗? 我的searchAd代码是这样的:

private String SearchAd(string ID) { DirectoryEntry ldapObj; DirectorySearcher searchObj; SearchResult entryObj; DirectoryEntry entry; string strValue = "No Entry Found"; string strSearchFor; ldapObj = new DirectoryEntry(); ldapObj.Username = @"XYZ"; ldapObj.Password =System.Configuration.ConfigurationManager.AppSettings["XXX"].Trim(); ldapObj.Path = "LDAP://XYZ/dc=XX,dc=XX,dc=XX"; ldapObj.AuthenticationType = AuthenticationTypes.Secure; searchObj = new DirectorySearcher(ldapObj); searchObj.Filter = "(samaccountname=" + ID + ")"; searchObj.ReferralChasing = ReferralChasingOption.All; searchObj.PropertiesToLoad.Add("homemdb"); try { entryObj = searchObj.FindOne(); try { // give me the homemdb attribute entry = entryObj.GetDirectoryEntry(); ResultPropertyCollection attributes; attributes = entryObj.Properties; strSearchFor = "homemdb"; strValue = Convert.ToString(attributes[strSearchFor][0]); //assuming only one homemdb value, could be an invalid assumption } catch { // Not really an error, but there was no entry found with this filter value strValue = "No entry found"; } } catch(Exception srhex) { Trace.Write("info", "Error occured. Error is:" + srhex.ToString()); writeLog("Active Direcory read exception: " + srhex.ToString()); strValue = "Error in searching Active Directory."; } searchObj.Dispose(); entryObj = null; ldapObj.Close(); ldapObj.Dispose(); searchObj = null; ldapObj = null; return strValue; } } private String Findbest(String ID) { string strLocation; string[] ExchangeInfo; strLocation = SearchAD(ID.Trim()); if (strLocation == "No entry found") { MobileSolutions.InfoBox(this, "No Exchange mailbox was found for this account. An Exchange e-mail account is required for BlackBerry service. Please visit @@@@ to request an Exchange e-mail account."); return "Not Found"; } else { //Trim the exchange server name to just the value between the parenteses in the RDN. ExchangeInfo = strLocation.Split('('); ExchangeInfo = ExchangeInfo[1].Split(')'); Trace.Write("info", "Exchange server found is: " + ExchangeInfo[0]); SqlClient.SqlDataReader SQLQuery; SqlConnection.DBName = "XXXXX"; SQLQuery = SQLCon.Connect("select BESInstance from ExchangeMap where Exchange = '" = ExchangeInfo[0] + "'"); if (SQLQuery.Read() == true) { return SQLQuery.GetValue[0]; } else { SendMail.Send("XXX","YYY", " website error", "A user (" + ID + ") tried to add an account, but no BES Instance was found for this users Exchange server: " + ExchangeInfo[0] + ". Please add the corresponding BES Instance for this Exchange server in the ExchangeMap on the website."); return SQLQuery.GetValue[0]; } } return "Not Found"; }