/// <summary> /// 保存导入的Bom信息 /// </summary> public void SaveImportBom(DataTable bomTable) { using (SqlConnection conn = new SqlConnection(BaseSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { SqlCommand cmd = new SqlCommand("", conn, trans); DateTime nowDate = BaseSQL.GetServerDateTime(); string nowLongDate = nowDate.ToString("yyyy-MM-dd HH:mm:ss"); //var query = from g in bomTable.AsEnumerable() // group g by new { t1 = g.Field<string>("MotherCodeFileName") } into cfnList // select new { MotherCodeFileName = cfnList.Key.t1, StallInfo = cfnList }; //foreach (var cfnInfo in query) //{ // string codeFileNameStr = cfnInfo.MotherCodeFileName; DataView dv = new DataView(bomTable); DataTable rootTable = dv.ToTable(true, "MotherCodeFileName"); foreach (DataRow rootDR in rootTable.Rows) { string codeFileNameStr = DataTypeConvert.GetString(rootDR["MotherCodeFileName"]); cmd.CommandText = string.Format("Insert into BS_BomManagement(MaterielNo, PartsCodeId, GetTime) select CodeFileName, AutoId, '{1}' from SW_PartsCode where CodeFileName = '{0}'", codeFileNameStr, nowLongDate); cmd.ExecuteNonQuery(); DataRow[] drs = bomTable.Select(string.Format("MotherCodeFileName='{0}'", codeFileNameStr)); foreach (DataRow dr in drs) { cmd.CommandText = string.Format("Insert into BS_BomMateriel(MaterielNo, LevelMaterielNo, Qty, GetTime) values ('{0}', '{1}', {2}, '{3}')", codeFileNameStr, DataTypeConvert.GetString(dr["SubCodeFileName"]), DataTypeConvert.GetInt(dr["Qty"]), nowLongDate); cmd.ExecuteNonQuery(); } } trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } finally { conn.Close(); } } } }
/// <summary> /// 保存Bom登记信息 /// </summary> public int SaveBomManagement(DataRow bomHeadRow, DataTable bomListTable) { using (SqlConnection conn = new SqlConnection(BaseSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { SqlCommand cmd = new SqlCommand("", conn, trans); DateTime nowTime = BaseSQL.GetServerDateTime(); if (bomHeadRow.RowState == DataRowState.Added)//新增 { cmd.CommandText = string.Format("select COUNT(*) from BS_BomManagement where MaterielNo = '{0}'", DataTypeConvert.GetString(bomHeadRow["MaterielNo"])); if (DataTypeConvert.GetInt(cmd.ExecuteScalar()) > 0) { //MessageHandler.ShowMessageBox("当前选择的零件名称已经存在Bom信息,不能重复设定,请重新输入。"); MessageHandler.ShowMessageBox(f.tsmiDqxzdl.Text); trans.Rollback(); return(0); } bomHeadRow["GetTime"] = nowTime; for (int i = 0; i < bomListTable.Rows.Count; i++) { bomListTable.Rows[i]["MaterielNo"] = bomHeadRow["MaterielNo"]; bomListTable.Rows[i]["GetTime"] = nowTime; } } else//修改 { for (int i = 0; i < bomListTable.Rows.Count; i++) { if (bomListTable.Rows[i].RowState == DataRowState.Deleted) { continue; } else if (bomListTable.Rows[i].RowState == DataRowState.Added) { bomListTable.Rows[i]["MaterielNo"] = bomHeadRow["MaterielNo"]; bomListTable.Rows[i]["GetTime"] = nowTime; } } } //保存日志到日志表中 //string logStr = LogHandler.RecordLog_DataRow(cmd, "Bom登记信息", bomHeadRow, "MaterielNo"); string logStr = LogHandler.RecordLog_DataRow(cmd, f.tsmiBomdjx.Text, bomHeadRow, "MaterielNo"); cmd.CommandText = "select * from BS_BomManagement where 1=2"; SqlDataAdapter adapterHead = new SqlDataAdapter(cmd); DataTable tmpHeadTable = new DataTable(); adapterHead.Fill(tmpHeadTable); BaseSQL.UpdateDataTable(adapterHead, bomHeadRow.Table); cmd.CommandText = "select * from BS_BomMateriel where 1=2"; SqlDataAdapter adapterList = new SqlDataAdapter(cmd); DataTable tmpListTable = new DataTable(); adapterList.Fill(tmpListTable); BaseSQL.UpdateDataTable(adapterList, bomListTable); trans.Commit(); return(1); } catch (Exception ex) { trans.Rollback(); bomHeadRow.Table.RejectChanges(); bomListTable.RejectChanges(); throw ex; } finally { conn.Close(); } } } }