/// <summary> /// 实体类插入数据库 必须手动加事务 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public bool InsertOrUpdateTList <T>(T t, InsertTypeEnum insertType) where T : class { List <T> tList = new List <T>(); tList.Add(t); return(InsertOrUpdateTList(tList, insertType)); }
/// <summary> /// 实体类插入数据库 必须手动加事务 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tList"></param> /// <returns>成功失败</returns> public bool InsertOrUpdateTList <T>(List <T> tList, InsertTypeEnum insertType) where T : class { if (tList.Count == 0) { return(true); } Type type = typeof(T); StringBuilder colsMergeWithoutIdentitySB = new StringBuilder(); StringBuilder colsMergeAssignmentSB = new StringBuilder(); StringBuilder colsMergeOnSB = new StringBuilder(); //列值 List <ORMInfo> ormList = base.GetORM(type); string tableName = ormList[0].TableName; ORMInfo.QuoteDic = new System.Collections.Concurrent.ConcurrentDictionary <string, bool>(); List <string> computedColumnList = new List <string>(); StringBuilder insertTempSB = new StringBuilder(); string createTempStr = string.Format(" select * into [#{0}] from [{0}] where 1!=1", tableName); insertTempSB.Append(createTempStr); bool hasIdentity = GetOrmInfo(ormList, out computedColumnList); if (hasIdentity) { insertTempSB.Insert(createTempStr.Length, string.Format("; \r\n set IDENTITY_INSERT [#{0}] on", tableName)); } foreach (string col in computedColumnList) { insertTempSB.Insert(createTempStr.Length, string.Format("; \r\n alter table [#{0}] drop column [{1}]", tableName, col)); } //创建临时表 ExecuteNonQuery(insertTempSB.ToString()); #region 数据放入临时表 StringBuilder insertTempDataSB = null; List <SqlParameter> pars = null; List <T> overTList = null; while (1 == 1) { if (overTList == null) { InitTempTable(tList, ormList, type, tableName, out insertTempDataSB, out pars, out overTList); ExecuteNonQuery(insertTempDataSB.ToString(), pars.ToArray()); } else if (overTList.Count > 0) { InitTempTable(overTList, ormList, type, tableName, out insertTempDataSB, out pars, out overTList); ExecuteNonQuery(insertTempDataSB.ToString(), pars.ToArray()); } else if (overTList.Count == 0) { break; } } #endregion //Merge数据 StringBuilder insertSB = new StringBuilder(); insertSB.Append(string.Format("; \r\n declare @countrow int; \r\n select @countrow = count(1) from [#{0}] ", tableName)); foreach (ORMInfo orm in ormList) { if (orm.IsComputedColumn) { continue; } if (!orm.IsIdentity) { colsMergeWithoutIdentitySB.Append(string.Format(",[{0}]", orm.ColumnName)); if (!orm.Primaykey) { colsMergeAssignmentSB.Append(string.Format(",[{0}]=[#{1}].[{0}] ", orm.ColumnName, tableName)); } } if (orm.Primaykey) { colsMergeOnSB.Append(string.Format(" and [#{0}].[{1}] =[{0}].[{1}] ", tableName, orm.ColumnName)); } } insertSB.Append("; \r\n if(@countrow!=0) \r\n begin"); insertSB.Append(string.Format(" ; \r\n merge into [{0}] using [#{0}] on 1=1 ", tableName)); insertSB.Append(colsMergeOnSB.ToString()); insertSB.Append("\r\n when not matched "); insertSB.Append(string.Format("\r\n then insert({0}) values({0}) ", colsMergeWithoutIdentitySB.Remove(0, 1).ToString())); if (insertType == InsertTypeEnum.InsertAndUpdate) { insertSB.Append("\r\n when matched "); insertSB.Append(string.Format("\r\n then update set {0} ", colsMergeAssignmentSB.Remove(0, 1).ToString())); } insertSB.Append(";\r\n end"); SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, insertSB.ToString(), CommandType.Text, pars.ToArray()); int line = 0; try { line = cmd.ExecuteNonQuery(); return(true); } catch (SqlException ex) { throw ex; } catch (Exception ex) { throw ex; } finally { CloseConn(); } }
private bool MoveNodeAsSibling(TreeViewItem dragSource, TreeViewItem parentIt, InsertTypeEnum itype, HashSet <TreeNode> modifiedSet) { bool rt = false; try { var n = this.ReadNode(dragSource); var pNode = this.ReadNode(parentIt); if (itype == CtlTreeView.InsertTypeEnum.NextSibling) { this._ctl.DbAccessor.InsertNewNodeBetween(n, pNode.ParentId, pNode, null, modifiedSet); } else if (itype == CtlTreeView.InsertTypeEnum.PrevSibling) { this._ctl.DbAccessor.InsertNewNodeBetween(n, pNode.ParentId, null, pNode, modifiedSet); } else { Util.Assert(false); } this.RemoveFromStartPosition(dragSource); int idx = this.GetIndexAtParent(parentIt); if (itype == InsertTypeEnum.NextSibling) { idx++; } this.GetBrotherItems(parentIt).Insert(idx, dragSource); rt = true; } catch (Exception e) { Log.Exception(e); } return(rt); }