static void Main(string[] args) { //string connectionEmployeeDatabase = "DSN=Test;Uid=walden;Pwd=walden"; string connectionMessagingDatabase = "Server=COS-DEV01\\SQLEXPRESS;Database=Messaging;Uid=sa;Pwd=0Griswold;"; List<EBSEmployee> employeeDataList = new List<EBSEmployee>(); EBSEmployee employeeData = new EBSEmployee(); var principalContext = new PrincipalContext(ContextType.Domain, "ct-ortho.com"); UserPrincipal userPrin = new UserPrincipal(principalContext); var searcher = new System.DirectoryServices.AccountManagement.PrincipalSearcher(); searcher.QueryFilter = userPrin; var results = searcher.FindAll(); foreach (Principal p in results) { UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, p.SamAccountName); employeeData = new EBSEmployee(); 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.UserID = p.Guid.ToString(); if (CheckToSeeIfUserExists(connectionMessagingDatabase, p.Guid.ToString())) { UpdateEmployeeRecords(connectionMessagingDatabase, employeeData); } else { InsertEmployeeRecords(connectionMessagingDatabase, employeeData); } } }
private static void UpdateEmployeeRecords(string connectionMessagingDatabase, EBSEmployee ebsEmployee) { using (SqlConnection db = new SqlConnection(connectionMessagingDatabase)) { try { const string query = "update Users" + " set FirstName = @FirstName" + " , LastName = @LastName" + " , UserName = @UserName" + " where UserID = @UserID"; int rowsAffectd = db.Execute(query, new { @UserID = ebsEmployee.UserID, @UserName = ebsEmployee.UserName, @FirstName = ebsEmployee.FirstName, @LastName = ebsEmployee.LastName, }); int r = rowsAffectd; //return true; } catch (Exception er) { string s1 = er.ToString(); //return false; //Log.LogMessage(er.ToString()); } } }
private static void InsertEmployeeRecords(string connectionMessagingDatabase, EBSEmployee ebsEmployee) { Guid id = Guid.NewGuid(); string s10 = id.ToString(); using (SqlConnection db = new SqlConnection(connectionMessagingDatabase)) { try { const string query = "INSERT INTO Users(" + " UserID,UserName,FirstName,LastName)VALUES(" + " @UserID,@UserName,@FirstName,@LastName)"; int rowsAffectd = db.Execute(query, new { @UserID = ebsEmployee.UserID, @UserName = ebsEmployee.UserName, @FirstName = ebsEmployee.FirstName, @LastName = ebsEmployee.LastName, }); int r = rowsAffectd; //return true; } catch (Exception er) { string s1 = er.ToString(); //return false; //Log.LogMessage(er.ToString()); } } }