private static void InsertEmployeeRecords(EBSEmployee ebsEmployee) { Guid id = Guid.NewGuid(); string s10 = id.ToString(); using (SqlConnection db = new SqlConnection(ConfigurationValues.PostOfficeDatabaseConnection)) { try { const string query = "INSERT INTO [ActiveDirectoryUsers]" + " (" + " [UserName]" + " ,[FirstName]" + " ,[LastName]" + " ,[UserGuid]" + " ,[HomeFolder]" + ")" + " VALUES" + " (" + " @UserName,@FirstName,@LastName,@UserGuid,@HomeFolder" + " )"; int rowsAffectd = db.Execute(query, new { @UserGuid = ebsEmployee.UserGuid, @UserName = ebsEmployee.UserName, @FirstName = ebsEmployee.FirstName, @LastName = ebsEmployee.LastName, @HomeFolder = ebsEmployee.HomeFolder }); int r = rowsAffectd; } catch (Exception er) { Log(er.ToString()); } } }
private void ServiceTimer_Tick(object sender, System.Timers.ElapsedEventArgs e) { try { this.timer.Stop(); DateTime now = DateTime.Now; if (now.ToShortTimeString().Substring(0, 1) == ConfigurationValues.HourToRun && now.ToShortTimeString().IndexOf(ConfigurationValues.AmPmToRun) > 0) { DeleteTable(); List<EBSEmployee> employeeDataList = new List<EBSEmployee>(); EBSEmployee employeeData = new EBSEmployee(); // create your domain context //PrincipalContext ctx = new PrincipalContext(ContextType.Domain); PrincipalContext ctx = new PrincipalContext(ContextType.Domain,"ct-ortho.com"); // define a "query-by-example" principal - here, we search for a GroupPrincipal GroupPrincipal qbeGroup = new GroupPrincipal(ctx); // create your principal searcher passing in the QBE principal PrincipalSearcher srch = new PrincipalSearcher(qbeGroup); // find all matches foreach (var found in srch.FindAll()) { GroupPrincipal foundGroup = found as GroupPrincipal; if (foundGroup != null) { // find the group in question (or load it from e.g. your list) GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, foundGroup.Name); // if found.... if (group != null) { // iterate over members foreach (Principal p in group.GetMembers()) //foreach (Principal p in group.GetMembers()) { try { UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(ctx, p.SamAccountName); if (userPrincipal != null) { Console.WriteLine(userPrincipal.SamAccountName); employeeData = new EBSEmployee(); if (string.IsNullOrEmpty(userPrincipal.HomeDirectory)) { employeeData.HomeFolder = string.Empty; } else { employeeData.HomeFolder = userPrincipal.HomeDirectory; } if (string.IsNullOrEmpty(userPrincipal.GivenName)) { employeeData.FirstName = string.Empty; } else { employeeData.FirstName = userPrincipal.GivenName; } if (string.IsNullOrEmpty(userPrincipal.Surname)) { employeeData.LastName = string.Empty; } else { employeeData.LastName = userPrincipal.Surname; } if (string.IsNullOrEmpty(p.SamAccountName)) { employeeData.UserName = string.Empty; } else { employeeData.UserName = p.SamAccountName; } employeeData.UserGuid = p.Guid.ToString(); if (CheckToSeeIfUserExists(p.Guid.ToString())) { UpdateEmployeeRecords(employeeData); } else { InsertEmployeeRecords(employeeData); } } } catch (Exception er) { string sw = er.ToString(); } } } } } } this.timer.Start(); } catch (Exception er) { Log(er.ToString()); this.timer.Start(); } }
private static void UpdateEmployeeRecords(EBSEmployee ebsEmployee) { using (SqlConnection db = new SqlConnection(ConfigurationValues.PostOfficeDatabaseConnection)) { try { const string query = "update ActiveDirectoryUsers" + " set FirstName = @FirstName" + " , LastName = @LastName" + " , UserName = @UserName" + " , HomeFolder = @HomeFolder" + " where UserGuid = @UserGuid"; int rowsAffectd = db.Execute(query, new { @UserGuid = ebsEmployee.UserGuid, @UserName = ebsEmployee.UserName, @FirstName = ebsEmployee.FirstName, @LastName = ebsEmployee.LastName, @HomeFolder = ebsEmployee.HomeFolder }); int r = rowsAffectd; //return true; } catch (Exception er) { string s1 = er.ToString(); //return false; //Log.LogMessage(er.ToString()); } } }