//public Department NewDepartment { get; } = new Department(); //public DepartmentViewModel() //{ // NewDepAdd(TextDep); //} private static void NewDepAdd(string text) { using (var db = new OrgDB()) { Department Department = new Department(); Department.NameDep = text; db.Departments.Add(Department); db.SaveChanges(); } }
public static void DBCheckEmployee() { using (var db = new OrgDB()) { db.Database.Log = str => Console.WriteLine("EF>> {0}", str); var students_count = db.Employees.Count(); Console.WriteLine("Emploees in DB: {0}", students_count); } }
private void Button_Click(object sender, RoutedEventArgs e) { using (var db = new OrgDB()) { Department Department = new Department(); Department.NameDep = TetxDep.Text; db.Departments.Add(Department); db.SaveChanges(); } MessageBox.Show("Подразделение добавлено"); }
private static void EmployeeFromDepartment() { using (var db = new OrgDB()) { db.Database.Log = str => Console.WriteLine("EF>> {0}", str); var Dep = db.Employees .Include(employee => employee.Department) .Where(employee => employee.Department.Id == 130).ToArray(); Console.WriteLine(Dep[1].Department.NameDep); } }
//void FillComboDox(List<string> AllDepartments) //{ // DepComboBox.ItemsSource = AllDepartments; //} private void Button_Click(object sender, RoutedEventArgs e) { using (var db = new OrgDB()) { Employee Employee = new Employee(); Employee.Surname = TextSurname.Text; Employee.Name = TextName.Text; Employee.Patronymic = TextSurname.Text; Employee.Salary = double.Parse(TextSalary.Text); //Employee.Department.NameDep = DepComboBox.Text; db.Employees.Add(Employee); db.SaveChanges(); } MessageBox.Show("Сотрудник добавлен"); }
private static List <Employee> AllOrgEmp() { List <Employee> emplist = new List <Employee>(); using (var db = new OrgDB()) { db.Database.Log = str => Console.WriteLine("EF>> {0}", str); var emp = db.Employees; foreach (var e in emp) { emplist.Add(e); } } return(emplist); }
private static List <Department> AllOrgDep() { List <Department> deplist = new List <Department>(); using (var db = new OrgDB()) { db.Database.Log = str => Console.WriteLine("EF>> {0}", str); var dep = db.Departments; foreach (var d in dep) { deplist.Add(d); } } return(deplist); }
private static void ImportEmp(List <Employee> list, List <string> dep) { int n = 0; if (list.Count == 0 || dep.Count == 0) { return; } using (var db = new OrgDB()) { if (!db.Departments.Any()) { for (int i = 0; i < dep.Count; i++) { var department = new Department(); { department.NameDep = dep[i]; }; for (int j = 0; j < 4; j++) { var employee = new Employee(); { employee.Surname = list[n].Surname; employee.Name = list[n].Name; employee.Patronymic = list[n].Patronymic; employee.Salary = list[n].Salary; }; department.Employees.Add(employee); n++; } db.Departments.Add(department); } } db.Database.Log = str => Console.WriteLine("EF>> {0}", str); db.SaveChanges(); } }
public string UploadUserFile(string filePath) { List <string> list = new List <string>(); string modePath = System.IO.Directory.GetCurrentDirectory() + "\\ExcelModel\\用户.xlsx"; //原始文件 string path = filePath; //原始文件 string mes = ""; DataTable dt = new DataTable(); UTILITY.ExcelTools tool = new UTILITY.ExcelTools(); tool.GetDataTable(System.IO.File.OpenRead(path), path, modePath, ref mes, ref dt); if (dt == null || dt.Rows.Count == 0) { return("空数据,导入失败!"); } //DataView dv = new DataView(dt); //String[] str = { "组织机构编码", "组织机构名称", "账号", "姓名", "员工编号", "性别", "办公电话", "手机", "邮箱", "访问IP", "账号类型","备注" }; //dt = dv.ToTable(true, str); //if (dt.Rows.Count != dv.ToTable(true, "账号").Rows.Count) //{ // return "账号列存在重复数据,导入失败!"; //} string error = GetDistinctSelf(dt, "账号"); if (error != null && error.Length > 0) { return(error); } int truckNum = Convert.ToInt32(Convert.ToDecimal(dt.Rows.Count / 500)); int yushu = dt.Rows.Count % 500; if (yushu > 0) { truckNum++; } for (int j = 1; j < truckNum + 1; j++) { string fengefu = ""; StringBuilder sb = new StringBuilder(); StringBuilder sbOrgUser = new StringBuilder(); sbOrgUser.Append("insert into ts_uidp_org_user(ORG_ID,USER_ID)values "); sb.Append(" INSERT INTO ts_uidp_userinfo(USER_ID,USER_DOMAIN,USER_CODE,USER_NAME,USER_PASS,PHONE_MOBILE,PHONE_OFFICE," + "USER_EMAIL,USER_IP,USER_SEX,AUTHENTICATION_TYPE,FLAG,REG_TIME,REMARK) values "); OrgDB orgDB = new OrgDB(); DataTable dtOrg = orgDB.fetchOrgList(); string result = ""; string fengefu2 = ""; int rowbegin = (j - 1) * 500; int rowend = j * 500; if (rowend > dt.Rows.Count) { rowend = dt.Rows.Count; } for (int i = rowbegin; i < rowend; i++) { if (dt.Rows[i]["组织机构编码"] == null || dt.Rows[i]["账号"] == null) { result += fengefu2 + "第" + (i + 2) + "行,组织机构编码或者账号不能为空!,导入失败!"; fengefu2 = ","; continue; } if (dt.Rows[i]["组织机构编码"].ToString() == "" || dt.Rows[i]["账号"].ToString() == "") { result += fengefu2 + "第" + (i + 2) + "行,组织机构编码或者账号不能为空!,导入失败!"; fengefu2 = ","; continue; } DataRow[] OrgRow = dtOrg.Select("ORG_CODE='" + dt.Rows[i]["组织机构编码"].ToString().Trim() + "'"); if (OrgRow.Length <= 0) { result += fengefu2 + "第" + (i + 2) + "行,系统中不存在此组织机构编码!,导入失败!"; fengefu2 = ","; continue; } string id = Guid.NewGuid().ToString(); sbOrgUser.Append(fengefu + "('" + dt.Rows[i]["组织机构编码"].ToString().Trim() + "','" + id + "')"); sb.Append(fengefu + "('" + id + "',"); sb.Append("'" + getString(dt.Rows[i]["账号"]) + "',"); sb.Append("'" + getString(dt.Rows[i]["员工编号"]) + "',"); sb.Append("'" + getString(dt.Rows[i]["姓名"]) + "',"); sb.Append("'123456',"); sb.Append("'" + getString(dt.Rows[i]["手机"]) + "',"); sb.Append("'" + getString(dt.Rows[i]["办公电话"]) + "',"); sb.Append("'" + getString(dt.Rows[i]["邮箱"]) + "',"); sb.Append("'" + getString(dt.Rows[i]["访问IP"]) + "',"); if (dt.Rows[i]["性别"] != null && dt.Rows[i]["性别"].ToString() == "男") { sb.Append("1,"); } else { sb.Append("0,"); } if (dt.Rows[i]["账号类型"] != null && dt.Rows[i]["账号类型"].ToString() == "PTR账号") { sb.Append("'1',"); } else { sb.Append("'0',"); } sb.Append("1,'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',"); sb.Append("'" + getString(dt.Rows[i]["备注"]) + "')"); fengefu = ","; } if (sbOrgUser != null && sbOrgUser.Length > 0) { list.Add(sbOrgUser.ToString()); } if (sb != null && sb.Length > 0) { list.Add(sb.ToString()); } } //string sqlUpdate = " update a set a.ORG_ID=b.ORG_ID from ts_uidp_org_user a ,ts_uidp_org b where a.ORG_ID=b.ORG_CODE "; //string sqlUpdate = "update ts_uidp_org_user a ,ts_uidp_org b set a.ORG_ID = b.ORG_ID where a.ORG_ID = b.ORG_CODE"; if (db.GetDBType() == "MYSQL") { string sqlUpdate = " update ts_uidp_org_user a ,ts_uidp_org b set a.ORG_ID = b.ORG_ID where a.ORG_ID = b.ORG_CODE"; list.Add(sqlUpdate); } else if (db.GetDBType() == "SQLSERVER") { string sqlUpdate = " update a set a.ORG_ID=b.ORG_ID from ts_uidp_org_user a ,ts_uidp_org b where a.ORG_ID=b.ORG_CODE "; list.Add(sqlUpdate); } else if (db.GetDBType() == "ORACLE") { string sqlUpdate = " update ts_uidp_org_user a ,ts_uidp_org b set a.ORG_ID = b.ORG_ID where a.ORG_ID = b.ORG_CODE"; list.Add(sqlUpdate); } return(db.UploadUserFile(list)); }
public string UploadUserFileNew(string filePath) { //string modePath = System.IO.Directory.GetCurrentDirectory() + "\\ExcelModel\\用户.xlsx";//原始文件 //string path = filePath;//原始文件 //string mes = ""; // //DataTable dt = new DataTable(); //UTILITY.ExcelTools tool = new UTILITY.ExcelTools(); //tool.GetDataTable(System.IO.File.OpenRead(path), path, modePath, ref mes, ref dt); List <string> list = new List <string>(); string modePath = System.IO.Directory.GetCurrentDirectory() + "\\ExcelModel\\用户.xls"; //原始文件 string path = filePath; //原始文件 string mes = ""; string result = ""; DataTable dt = new DataTable(); DataTable userdt = db.fetchUserList(); UTILITY.ExcelTools tool = new UTILITY.ExcelTools(); tool.GetDataTable(System.IO.File.OpenRead(path), path, modePath, ref mes, ref dt); if (dt == null || dt.Rows.Count == 0) { return("空数据,导入失败!"); } string error = GetDistinctSelf(dt, "账号"); //if (error != null && error.Length > 0) //{ // return error; //} int truckNum = Convert.ToInt32(Convert.ToDecimal(dt.Rows.Count / 500)); int yushu = dt.Rows.Count % 500; if (yushu > 0) { truckNum++; } for (int j = 1; j < truckNum + 1; j++) { string fengefu = ""; //StringBuilder sb = new StringBuilder(); //StringBuilder sbOrgUser = new StringBuilder(); //sbOrgUser.Append("insert into ts_uidp_org_user(ORG_ID,USER_ID)values "); //sb.Append(" INSERT INTO ts_uidp_userinfo(USER_ID,USER_DOMAIN,USER_CODE,USER_NAME,USER_PASS,PHONE_MOBILE,PHONE_OFFICE," + // "USER_EMAIL,USER_IP,USER_SEX,AUTHENTICATION_TYPE,FLAG,REG_TIME,REMARK) values "); OrgDB orgDB = new OrgDB(); DataTable dtOrg = orgDB.fetchOrgList(); string fengefu2 = ""; int rowbegin = (j - 1) * 500; int rowend = j * 500; if (rowend > dt.Rows.Count) { rowend = dt.Rows.Count; } for (int i = rowbegin; i < rowend; i++) { var usercode = getString(dt.Rows[i]["账号"]); DataRow[] rows = userdt.Select("USER_DOMAIN='" + usercode + "'"); if (dt.Rows[i]["组织机构编码"] == null || dt.Rows[i]["账号"] == null) { result += fengefu2 + "第" + (i + 2) + "行,组织机构编码或者账号不能为空!,导入失败!"; fengefu2 = ","; continue; } if (dt.Rows[i]["组织机构编码"].ToString() == "" || dt.Rows[i]["账号"].ToString() == "") { result += fengefu2 + "第" + (i + 2) + "行,组织机构编码或者账号不能为空!,导入失败!"; fengefu2 = ","; continue; } if (dt.Rows[i]["账号类型"] == null || dt.Rows[i]["账号类型"].ToString() == "") { result += fengefu2 + "第" + (i + 2) + "行,账号类型不能为空!,导入失败!"; fengefu2 = ","; continue; } if (dt.Rows[i]["用户类型"] == null || dt.Rows[i]["用户类型"].ToString() == "") { result += fengefu2 + "第" + (i + 2) + "行,用户类型不能为空!,导入失败!"; fengefu2 = ","; continue; } if (dt.Rows[i]["员工姓名"] == null || dt.Rows[i]["员工姓名"].ToString() == "") { result += fengefu2 + "第" + (i + 2) + "行,员工姓名不能为空!,导入失败!"; fengefu2 = ","; continue; } if (dt.Rows[i]["性别"] == null || dt.Rows[i]["性别"].ToString() == "") { result += fengefu2 + "第" + (i + 2) + "行,性别不能为空!,导入失败!"; fengefu2 = ","; continue; } DataRow[] OrgRow = dtOrg.Select("ORG_CODE='" + dt.Rows[i]["组织机构编码"].ToString().Trim() + "'"); if (OrgRow.Length <= 0) { result += fengefu2 + "第" + (i + 2) + "行,系统中不存在此组织机构编码!,导入失败!"; fengefu2 = ","; continue; } if (rows.Length == 0) { StringBuilder sbOrgUser = new StringBuilder("insert into ts_uidp_org_user(ORG_ID,USER_ID)values"); string id = Guid.NewGuid().ToString(); sbOrgUser.Append("('" + dt.Rows[i]["组织机构编码"].ToString().Trim() + "','" + id + "')"); list.Add(sbOrgUser.ToString()); StringBuilder sb = new StringBuilder(" INSERT INTO ts_uidp_userinfo(USER_ID,AUTHENTICATION_TYPE,USER_DOMAIN,USER_TYPE,USER_PASS,USER_NAME,USER_CODE,USER_SEX,PHONE_OFFICE,PHONE_MOBILE," + "USER_EMAIL,FLAG,REG_TIME,REMARK) values "); sb.Append(fengefu + "('" + id + "',"); if (dt.Rows[i]["账号类型"] != null && dt.Rows[i]["账号类型"].ToString() == "PTR账号") { sb.Append("1,"); } else { sb.Append("0,"); } sb.Append("'" + getString(dt.Rows[i]["账号"]) + "',"); if (dt.Rows[i]["用户类型"] != null && dt.Rows[i]["用户类型"].ToString() == "普通用户") { sb.Append("1,"); } else { sb.Append("0,"); } sb.Append("'"); sb.Append(getString(dt.Rows[i]["用户密码"]) == "" ? UIDP.Security.SecurityHelper.StringToMD5Hash("123456") : UIDP.Security.SecurityHelper.StringToMD5Hash(getString(dt.Rows[i]["用户密码"]))); sb.Append("',"); sb.Append("'" + getString(dt.Rows[i]["员工姓名"]) + "',"); sb.Append("'" + getString(dt.Rows[i]["员工编号"]) + "',"); if (dt.Rows[i]["性别"] != null && dt.Rows[i]["性别"].ToString() == "男") { sb.Append("1,"); } else { sb.Append("0,"); } sb.Append("'" + getString(dt.Rows[i]["办公电话"]) + "',"); sb.Append("'" + getString(dt.Rows[i]["手机"]) + "',"); sb.Append("'" + getString(dt.Rows[i]["电子邮箱"]) + "',"); if (dt.Rows[i]["账号状态"] != null && dt.Rows[i]["账号状态"].ToString() == "禁用") { sb.Append("0,"); } else { sb.Append("1,"); } sb.Append("TO_DATE('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-mm-dd hh24:mi:ss'),"); sb.Append("'" + getString(dt.Rows[i]["备注"]) + "')"); //fengefu = ","; list.Add(sb.ToString()); } else { foreach (var item in rows) { string sql = "update ts_uidp_userinfo set "; sql += " AUTHENTICATION_TYPE="; sql += getString((dt.Rows[i]["账号类型"] != null && dt.Rows[i]["账号类型"].ToString() == "PTR账号") ? 1 : 0) + ","; sql += " USER_DOMAIN='" + getString(dt.Rows[i]["账号"]) + "',"; sql += " USER_TYPE="; sql += getString((dt.Rows[i]["用户类型"] != null && dt.Rows[i]["用户类型"].ToString() == "普通用户") ? 1 : 0) + ","; sql += " USER_PASS='******'," : UIDP.Security.SecurityHelper.StringToMD5Hash(getString(dt.Rows[i]["用户密码"])) + "',"; sql += " USER_NAME='" + getString(dt.Rows[i]["员工姓名"]) + "',"; sql += " USER_CODE='" + getString(dt.Rows[i]["员工编号"]) + "',"; sql += " USER_SEX="; sql += getString((dt.Rows[i]["性别"] != null && dt.Rows[i]["性别"].ToString() == "男") ? 1 : 0) + ","; sql += " PHONE_MOBILE='" + getString(dt.Rows[i]["手机"]) + "',"; sql += " PHONE_OFFICE='" + getString(dt.Rows[i]["办公电话"]) + "',"; sql += " USER_EMAIL='" + getString(dt.Rows[i]["电子邮箱"]) + "',"; sql += " FLAG="; sql += getString((dt.Rows[i]["账号状态"] != null && dt.Rows[i]["账号状态"].ToString() == "禁用") ? 0: 1) + ","; sql += " REG_TIME=TO_DATE('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-mm-dd hh24:mi:ss'),"; sql += " REMARK='" + getString(dt.Rows[i]["备注"]) + "'"; sql += " where USER_ID='" + item["USER_ID"].ToString() + "'"; list.Add(sql); string sql2 = "update ts_uidp_org_user set ORG_ID=(SELECT ORG_ID FROM ts_uidp_org where ORG_CODE='" + dt.Rows[i]["组织机构编码"].ToString().Trim() + "') where USER_ID='" + item["USER_ID"].ToString() + "'"; list.Add(sql2); } } //if (sbOrgUser != null && sbOrgUser.Length > 0) //{ // list.Add(sbOrgUser.ToString()); //} //if (sb != null && sb.Length > 0) //{ // list.Add(sb.ToString()); //} } //if (sb.Length > 0) //{ // sb.Insert(0, " INSERT INTO ts_uidp_userinfo(USER_ID,AUTHENTICATION_TYPE,USER_DOMAIN,USER_TYPE,USER_PASS,USER_NAME,USER_CODE,USER_SEX,PHONE_OFFICE,PHONE_MOBILE," + // "USER_EMAIL,FLAG,REG_TIME,REMARK) values "); //} //if (sbOrgUser.Length > 0) //{ // sbOrgUser.Insert(0, " insert into ts_uidp_org_user(ORG_ID,USER_ID)values "); //} //if (sb != null && sb.Length > 0) //{ // list.Add(sb.ToString()); //} //if (sbOrgUser != null && sbOrgUser.Length > 0) //{ // list.Add(sbOrgUser.ToString()); //} } if (db.GetDBType() == "MYSQL") { string sqlUpdate = " update ts_uidp_org_user a ,ts_uidp_org b set a.ORG_ID = b.ORG_ID where a.ORG_ID = b.ORG_CODE"; list.Add(sqlUpdate); } else if (db.GetDBType() == "SQLSERVER") { string sqlUpdate = " update a set a.ORG_ID=b.ORG_ID from ts_uidp_org_user a ,ts_uidp_org b where a.ORG_ID=b.ORG_CODE "; list.Add(sqlUpdate); } else if (db.GetDBType() == "ORACLE") { //string sqlUpdate = " update ts_uidp_org_user a ,ts_uidp_org b set a.ORG_ID = b.ORG_ID where a.ORG_ID = b.ORG_CODE"; //string sqlUpdate = "UPDATE TS_UIDP_ORG_USER a SET a.ORG_ID = (SELECT ORG_ID FROM TS_UIDP_ORG b WHERE a.ORG_ID = b.ORG_CODE)"; //list.Add(sqlUpdate); } if (result != "") { return(result); } else { return(db.UploadUserFile(list)); } //DataView dv = new DataView(dt); //if (dt.Rows.Count != dv.ToTable(true, "账号").Rows.Count) //{ // return "账号列存在重复数据,导入失败!"; //} //List<string> list = new List<string>(); //string fengefu = ""; //StringBuilder sb = new StringBuilder(); //StringBuilder sbOrgUser = new StringBuilder(); //string result = ""; //string fengefu2 = ""; //for (int i = 0; i < dt.Rows.Count; i++) //{ // var usercode = getString(dt.Rows[i]["账号"]); // var dtt = userdt; // DataRow[] rows = userdt.Select("USER_DOMAIN='" + usercode + "'"); // if (rows.Length == 0) // { // //sb.Append(" insert into ts_uidp_org (ORG_ID,ORG_CODE,ORG_NAME,ORG_SHORT_NAME,ORG_CODE_UPPER,ISINVALID,ISDELETE,REMARK) values "); // string id = Guid.NewGuid().ToString(); // sbOrgUser.Append(fengefu + "('" + dt.Rows[i]["组织机构编码"].ToString().Trim() + "','" + id + "')"); // sb.Append(fengefu + "('" + id + "',"); // sb.Append("'" + getString(dt.Rows[i]["账号"]) + "',"); // sb.Append("'" + getString(dt.Rows[i]["员工编号"]) + "',"); // sb.Append("'" + getString(dt.Rows[i]["姓名"]) + "',"); // sb.Append("'123456',"); // sb.Append("'" + getString(dt.Rows[i]["手机"]) + "',"); // sb.Append("'" + getString(dt.Rows[i]["办公电话"]) + "',"); // sb.Append("'" + getString(dt.Rows[i]["邮箱"]) + "',"); // sb.Append("'" + getString(dt.Rows[i]["访问IP"]) + "',"); // if (dt.Rows[i]["性别"] != null && dt.Rows[i]["性别"].ToString() == "男") // { // sb.Append("1,"); // } // else // { // sb.Append("0,"); // } // if (dt.Rows[i]["账号类型"] != null && dt.Rows[i]["账号类型"].ToString() == "PTR账号") // { // sb.Append("'1',"); // } // else // { // sb.Append("'0',"); // } // sb.Append("1,'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',"); // sb.Append("'" + getString(dt.Rows[i]["备注"]) + "')"); // fengefu = ","; // } // else // { // foreach (var item in rows) // { // string sql = "update ts_uidp_org set "; // sql += " USER_DOMAIN='" + getString(dt.Rows[i]["组织机构编码"]) + "',"; // sql += " USER_CODE='" + getString(dt.Rows[i]["组织机构编码"]) + "',"; // sql += " USER_NAME='" + getString(dt.Rows[i]["组织机构编码"]) + "',"; // sql += " USER_PASS='******',"; // sql += " PHONE_MOBILE='" + getString(dt.Rows[i]["组织机构编码"]) + "',"; // sql += " PHONE_OFFICE='" + getString(dt.Rows[i]["组织机构编码"]) + "',"; // sql += " USER_EMAIL='" + getString(dt.Rows[i]["组织机构名称"]) + "',"; // sql += " USER_IP='" + getString(dt.Rows[i]["组织机构简称"]) + "',"; // sql += " USER_SEX='" + getString(dt.Rows[i]["上级组织机构编码"]) + "',"; // sql += " AUTHENTICATION_TYPE='" + getString((row["是否有效"] != null && row["是否有效"].ToString() == "是") ? 1 : 0) + "',"; // sql += " FLAG='" + getString(row["上级组织机构编码"]) + "',"; // sql += " REG_TIME='" + getString(row["上级组织机构编码"]) + "',"; // sql += " REMARK='" + getString(row["备注"]) + "'"; // sql += " where USER_ID='" + item["USER_ID"].ToString() + "' ;"; // list.Add(sql); // } // } // //sqllst.Add(sb.ToString()); //} //if (sb.Length > 0) //{ // sb.Insert(0, " INSERT INTO ts_uidp_userinfo(USER_ID,USER_DOMAIN,USER_CODE,USER_NAME,USER_PASS,PHONE_MOBILE,PHONE_OFFICE," + // "USER_EMAIL,USER_IP,USER_SEX,AUTHENTICATION_TYPE,FLAG,REG_TIME,REMARK) values "); // sqllst.Add(sb.ToString()); //} //if (sbOrgUser.Length > 0) //{ // sb.Insert(0, " insert into ts_uidp_org_user(ORG_ID,USER_ID)values "); // sqllst.Add(sb.ToString()); //} //string sqlUpdate = "update ts_uidp_org_user a ,ts_uidp_org b set a.ORG_ID = b.ORG_ID where a.ORG_ID = b.ORG_CODE"; //list.Add(sbOrgUser.ToString()); //list.Add(sb.ToString()); //list.Add(sqlUpdate); //return db.UploadUserFile(list); }