/// <summary> /// 向数据库添加一条记录 /// </summary> /// <param name="dt">要添加的矿物所属表</param> /// <param name="chineseName">矿物名称 用来检测是否重名</param> /// <param name="mineral">要添加的矿物信息</param> /// <param name="type">决定更新DataGrid的参数</param> private void AddMineralToDB(DataTable dt, string chineseName, IMineral mineral, int type) { try { DataRow[] dataRow = dt.Select("ChineseName='" + chineseName + "'"); if (dataRow.Length > 0) { MessageBox.Show("已存在 ‘" + chineseName + "’ 矿物"); modifyNum++; } else { //开始增加矿物 并更新当前内存中的信息 AccessDB.Add(mineral); //DataGrid.ItemsSource正在使用时无法操作 InitDataTable(); UpdateCollections(); if (type == 1) { InitDataGridByCollection(OrginHomoMinerals); } else if (type == 2) { InitDataGridByCollection(OrginHeteMinerals); } else { InitDataGridByCollection(OrginMinerals); } modifyNum = 0; } IsAdd = false; } catch (Exception) { } }
/// <summary> /// 将DataTable的数据导入到数据库 /// </summary> /// <param name="filepath"></param> /// <param name="dt"></param> public static string ExportDataToAccess(string filepath, DataSet mineralDataSet) { int successed = 0; int failed = 0; DataTable dt = GetDataByExcel(filepath); if (dt.Rows.Count > 0) { if (CheckIsRepeatInExcel(dt)) { return("Excel中有重复的矿物,请检查后再来"); } if (dt.TableName.Contains("非均质矿物")) { for (int i = 0; i < dt.Rows.Count; i++) { DataRow[] dataRow = mineralDataSet.Tables[dt.TableName].Select("ChineseName='" + dt.Rows[i]["矿物中文名称"].ToString() + "'"); if (dataRow.Length > 0) { failed++; } else { try { HeterogeneousMineralInfo heterogeneousMineral = null; string Ar = dt.Rows[i]["非均质视旋转角Ar"].ToString(); if (!String.IsNullOrEmpty(Ar)) { if (Ar.Contains("=")) { Ar = Ar.Split('=')[1]; } Ar = AngleHelper.ConvertAngleToString(Ar); } heterogeneousMineral = new HeterogeneousMineralInfo(0, dt.Rows[i]["矿物中文名称"].ToString(), dt.Rows[i]["矿物英文名称"].ToString(), dt.Rows[i]["化学式"].ToString(), dt.Rows[i]["矿物的晶系"].ToString(), dt.Rows[i]["均非性"].ToString(), dt.Rows[i]["反射率"].ToString(), dt.Rows[i]["硬度"].ToString(), dt.Rows[i]["反射色"].ToString(), dt.Rows[i]["双反射及反射多色性"].ToString(), Ar, dt.Rows[i]["非均质视旋转色散DAr"].ToString(), dt.Rows[i]["旋向Rs"].ToString(), dt.Rows[i]["相符Ps"].ToString(), dt.Rows[i]["反射视旋转色散DRr"].ToString(), dt.Rows[i]["反射视旋转色散DAR"].ToString(), dt.Rows[i]["内反射"].ToString(), dt.Rows[i]["矿物成因产状形态特征及伴生矿物"].ToString(), dt.Rows[i]["主要鉴定特征"].ToString()); AccessDB.Add(heterogeneousMineral); successed++; } catch (Exception) { return("错误"); } } } } else { for (int i = 0; i < dt.Rows.Count; i++) { DataRow[] dataRow = mineralDataSet.Tables[dt.TableName].Select("ChineseName='" + dt.Rows[i]["矿物中文名称"].ToString() + "'"); if (dataRow.Length > 0) { failed++; } else { try { HomogeneousMineralInfo homogeneousMineral = null; string Rr = dt.Rows[i]["反射视旋转角Rr"].ToString(); if (!String.IsNullOrEmpty(Rr)) { if (Rr.Contains("=")) { Rr = Rr.Split('=')[1]; } Rr = AngleHelper.ConvertAngleToString(Rr); } homogeneousMineral = new HomogeneousMineralInfo(0, dt.Rows[i]["矿物中文名称"].ToString(), dt.Rows[i]["矿物英文名称"].ToString(), dt.Rows[i]["化学式"].ToString(), dt.Rows[i]["矿物的晶系"].ToString(), dt.Rows[i]["均非性"].ToString(), dt.Rows[i]["反射率"].ToString(), dt.Rows[i]["硬度"].ToString(), dt.Rows[i]["反射色"].ToString(), Rr, dt.Rows[i]["反射视旋转色散DRr"].ToString(), dt.Rows[i]["内反射"].ToString(), dt.Rows[i]["矿物成因产状形态特征及伴生矿物"].ToString(), dt.Rows[i]["主要鉴定特征"].ToString()); AccessDB.Add(homogeneousMineral); successed++; } catch (Exception) { return("错误"); } } } } return("数据导入到" + dt.TableName + "表成功!成功导入" + successed + "条,有" + failed + "条在数据库中已存在"); } else { return("请检查你的Excel中是否存在数据"); } }