/// <summary> /// 执行单条SQL语句 /// 需外部提交 /// </summary> /// <param name="trans">事务对象</param> /// <param name="sql">数据操作语句</param> /// <returns>语句执行影响记录数量</returns> public int Execute(ITrans trans, String sql) { Debug.Assert(!string.IsNullOrEmpty(sql), "要执行的SQL为空!"); Debug.Assert(trans != null, "事务对象为空!"); try { using (DbCommand command = m_dbFactory.CreateCommand()) { //设置默认命令超时时间为3分钟 程建波 添加于 2013-04-01 //command.CommandTimeout = 180; //设置为无限期等待 程建波 添加于 2013-04-17 command.CommandTimeout = 0; command.Transaction = trans.DbTrans; command.Connection = trans.DbConnection; command.CommandText = sql; int result = command.ExecuteNonQuery(); if (result == -1) { return(0); } return(result); } } catch (Exception ex) { Debug.Assert(false, ex.Message); return(-1); } }
// [李睿2014-8-13]添加带事务的查询,解决事务中无法查询插入的结果的问题 /// <summary> /// 执行单条查询语句得到数据表 /// *带异常处理 /// </summary> /// <param name="trans">事务对象</param> /// <param name="sql">查询语句</param> /// <returns>数据表</returns> public System.Data.DataTable QueryTable(ITrans trans, string sql) { AssertQuery(sql); DbCommand command = null; try { using (command = m_dbFactory.CreateCommand()) { // 命令对象 command.Connection = trans.DbConnection; command.Transaction = trans.DbTrans; command.CommandText = sql; // 数据操作对象 DbDataAdapter adapter = m_dbFactory.CreateDataAdapter(); adapter.SelectCommand = command; adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; DataTable dt = new DataTable(); adapter.Fill(dt); return(dt); } } catch (Exception ex) { Debug.Assert(false, ex.Message); return(null); } }
/// <summary> /// 得到通用的主键 /// </summary> /// <param name="TableName">表名</param> /// <param name="KeyFileid">字段名</param> /// <param name="tran">事务</param> /// <returns>返回最大ID</returns> public static int GetMaxID(string TableName, string KeyFileid, ITrans tran) { Debug.Assert(!string.IsNullOrEmpty(TableName), "表 " + TableName + " 不存在!"); Debug.Assert(!string.IsNullOrEmpty(KeyFileid), "主键ID为空!"); Debug.Assert(tran != null, "tran为空!"); string sql = string.Format("select max({0}) FROM {1}", KeyFileid, TableName); string ID = tran.Database.QueryScalar(sql).ToString(); return(ID == "" ? 1 : int.Parse(ID) + 1); }
private static void Main(string[] args) { ITrans[] trans = new ITrans[3]; trans[0] = new Coach("21903910", 30); trans[1] = new Train("21944951", 500); trans[2] = new Plant("00411234", 100); for (int i = 0; i < 3; i++) { trans[i].Run(); trans[i].DisplayInfo(); Console.WriteLine("-------------------"); } }
/// <summary> /// Assigns the tran property values REST. /// </summary> /// <param name="context">The context.</param> /// <param name="web">The web.</param> /// <param name="items">The items.</param> /// <param name="itemType">Type of the item.</param> /// <returns>List of Transaction data</returns> private List <ITrans> AssignTranPropertyValuesREST(ClientContext context, Web web, JArray items, Type itemType) { List <ITrans> tranItems = new List <ITrans>(); foreach (JToken item in items) { ITrans tranItem = Activator.CreateInstance(itemType) as ITrans; tranItem = this.SetTranPropertiesREST(context, web, item, tranItem, tranItem.GetType().GetProperties()); tranItem.ItemAction = ItemActionStatus.NOCHANGE; tranItem.ID = Convert.ToInt32(item["ID"]); tranItem.Index = tranItems.Count + 1; tranItems.Add(tranItem); } return(tranItems); }
/// <summary> /// Gets the transaction list data by identifier. /// </summary> /// <param name="context">The context.</param> /// <param name="web">The web.</param> /// <param name="type">The type.</param> /// <param name="listName">Name of the list.</param> /// <param name="transId">The trans identifier.</param> /// <param name="getSubItems">if set to <c>true</c> [get sub items].</param> /// <returns>Itrans object</returns> public ITrans GetTransactionListDataById(ClientContext context, Web web, Type type, string listName, int transId, bool getSubItems = true) { ITrans tranItem = null; if (context != null && web != null && type != null && transId > 0 && !string.IsNullOrEmpty(listName)) { tranItem = Activator.CreateInstance(type) as ITrans; List transList = web.Lists.GetByTitle(listName); ListItem transListItem = transList.GetItemById(transId); context.Load(transListItem); context.ExecuteQuery(); tranItem = this.SetTranProperties(context, web, transListItem, tranItem, tranItem.GetType().GetProperties(), getSubItems); } return(tranItem); }
/// <summary> /// 执行多条SQL语句(事务执行) /// 需外部提交 /// </summary> /// <param name="trans">事务对象</param> /// <param name="sqls">数据操作语句集合</param> /// <returns>语句执行影响记录数量</returns> public int Execute(ITrans trans, ICollection <String> sqls) { Debug.Assert(trans != null, "事务对象为空!"); Debug.Assert(sqls != null, "SQL语句集合为空!"); try { using (DbCommand command = m_dbFactory.CreateCommand()) { //默认命令超时时间为3分钟 程建波 添加于 2013-04-01 //command.CommandTimeout = 180; //设置为无限期等待 程建波 添加于 2013-04-17 command.CommandTimeout = 0; command.Transaction = trans.DbTrans; command.Connection = trans.DbConnection; int exeCount = 0; StringBuilder sb = new StringBuilder(); foreach (String sql in sqls) { sb.Append(sql); sb.Append(";"); // 命令对象 } command.CommandText = sb.ToString();; exeCount += command.ExecuteNonQuery(); if (exeCount == -1) { return(0); } return(exeCount); } } catch (Exception ex) { Debug.Assert(false, ex.Message); return(-1); } }
public TransContext(ITrans trans) { Trans = trans; }
public void Setup() { _trans = new Trans18000(); _ticket = Substitute.For<ITicket>(); }
/// <summary> /// Updates the tran item. /// </summary> /// <param name="context">The context.</param> /// <param name="web">The web.</param> /// <param name="item">The item.</param> /// <param name="tran">The tran.</param> /// <param name="objParams">The object parameters.</param> /// <returns> /// return true false /// </returns> private bool UpdateTranItem(ClientContext context, Web web, ref ListItem item, ITrans tran, Dictionary <string, string> objParams) { bool hasFile = false; PropertyInfo[] itemProperties = tran.GetType().GetProperties(); List <FileDetails> files = null; List <string> subTasklistNames = new List <string>(); List <List <ITask> > subTasks = new List <List <ITask> >(); List <string> transListName = new List <string>(); List <List <ITrans> > transList = new List <List <ITrans> >(); Dictionary <string, object> itemValues = new Dictionary <string, object>(); foreach (PropertyInfo property in itemProperties) { bool isListCoumn = property.GetCustomAttribute <IsListColumnAttribute>() == null || property.GetCustomAttribute <IsListColumnAttribute>().IsListColumn; bool isTask = property.GetCustomAttribute <IsTaskAttribute>() != null && property.GetCustomAttribute <IsTaskAttribute>().IsTaskField; bool isTran = property.GetCustomAttribute <IsTranAttribute>() != null && property.GetCustomAttribute <IsTranAttribute>().IsTranField; bool isFile = property.GetCustomAttribute <IsFileAttribute>() != null && property.GetCustomAttribute <IsFileAttribute>().IsFile; bool isPerson = property.GetCustomAttribute <IsPersonAttribute>() != null && property.GetCustomAttribute <IsPersonAttribute>().IsPerson; bool isReturnName = property.GetCustomAttribute <IsPersonAttribute>() != null && property.GetCustomAttribute <IsPersonAttribute>().ReturnName; string listCoumnName = property.GetCustomAttribute <FieldColumnNameAttribute>() != null && !string.IsNullOrEmpty(property.GetCustomAttribute <FieldColumnNameAttribute>().FieldsInformation) ? property.GetCustomAttribute <FieldColumnNameAttribute>().FieldsInformation : property.Name; if (isListCoumn) { if (isPerson) { if (!isReturnName) { string users = Convert.ToString(property.GetValue(tran)).Trim(','); itemValues[listCoumnName] = this.GetMultiplePersonField(context, web, users, property); } } else if (isFile) { files = property.GetValue(tran) != null?property.GetValue(tran) as List <FileDetails> : null; } else { itemValues[listCoumnName] = property.GetValue(tran); } } else if (isTran) { string tranListName = property.GetCustomAttribute <IsTranAttribute>().TranListName; List <ITrans> tranList = property.GetValue(tran) as List <ITrans>; if (tranList != null && tranList.Count > 0 && !string.IsNullOrEmpty(tranListName)) { transListName.Add(tranListName); transList.Add(tranList); } } else if (isTask) { string subTasklistName = property.GetCustomAttribute <IsTaskAttribute>().TaskListName; List <ITask> subTask = property.GetValue(tran) != null?property.GetValue(tran) as List <ITask> : null; if (!string.IsNullOrEmpty(subTasklistName) && subTask != null && subTask.Count > 0) { subTasklistNames.Add(subTasklistName); subTasks.Add(subTask); } } } foreach (KeyValuePair <string, object> itemValue in itemValues) { item[itemValue.Key] = itemValue.Value; } item.Update(); context.Load(item); context.ExecuteQuery(); if (files != null) { this.SaveAttachment(context, files, ref item); hasFile = true; } if (transList != null && transList.Count > 0 && transList.Count == transListName.Count) { string itemId = item["ID"].ToString(); for (int i = 0; i < subTasks.Count; i++) { if (transList[i] != null && transList[i].Count > 0) { transList[i].ForEach(p => p.RequestID = Convert.ToInt32(itemId)); this.SaveTranItems(context, web, transList[i], transListName[i], objParams); } } } return(hasFile); }
/// <summary> /// Sets the tran properties REST. /// </summary> /// <param name="context">The context.</param> /// <param name="web">The web.</param> /// <param name="listItem">The list item.</param> /// <param name="tranItem">The tran item.</param> /// <param name="properties">The properties.</param> /// <param name="getSubItems">if set to <c>true</c> [get sub items].</param> /// <returns> /// Transaction item /// </returns> private ITrans SetTranPropertiesREST(ClientContext context, Web web, JToken listItem, ITrans tranItem, PropertyInfo[] properties, bool getSubItems = true) { BELDataAccessLayer helper = new BELDataAccessLayer(); foreach (PropertyInfo property in properties) { try { bool isListColumn = property.GetCustomAttribute <IsListColumnAttribute>() == null || property.GetCustomAttribute <IsListColumnAttribute>().IsListColumn; bool isTask = property.GetCustomAttribute <IsTaskAttribute>() != null && property.GetCustomAttribute <IsTaskAttribute>().IsTaskField; bool isTran = property.GetCustomAttribute <IsTranAttribute>() != null && property.GetCustomAttribute <IsTranAttribute>().IsTranField; string listCoumnName = property.GetCustomAttribute <FieldColumnNameAttribute>() != null && !string.IsNullOrEmpty(property.GetCustomAttribute <FieldColumnNameAttribute>().FieldsInformation) ? property.GetCustomAttribute <FieldColumnNameAttribute>().FieldsInformation : property.Name; bool isFile = property.GetCustomAttribute <IsFileAttribute>() != null && property.GetCustomAttribute <IsFileAttribute>().IsFile; if (isListColumn) { if (property.GetCustomAttribute <FieldColumnNameAttribute>() != null && property.GetCustomAttribute <FieldColumnNameAttribute>().IsLookup) { string lookupFieldName = property.GetCustomAttribute <FieldColumnNameAttribute>().LookupFieldNameForTrans; if (property.GetCustomAttribute <FieldColumnNameAttribute>().IsMultipleLookup) { if (((JContainer)listItem[listCoumnName])["results"] != null) { List <string> reletedto = new List <string>(); foreach (dynamic lookup in ((JContainer)listItem[listCoumnName])["results"]) { reletedto.Add(lookup[lookupFieldName].ToString()); } property.SetValue(tranItem, reletedto); } } else { if (((dynamic)listItem[listCoumnName])[lookupFieldName] != null) { object value = ((JValue)((dynamic)listItem[listCoumnName])[lookupFieldName]).Value; Type t = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; object safeValue = (value == null) ? null : Convert.ChangeType(value, t); property.SetValue(tranItem, safeValue); } } } else if (property.GetCustomAttribute <IsPersonAttribute>() != null && property.GetCustomAttribute <IsPersonAttribute>().IsPerson) { if (property.GetCustomAttribute <IsPersonAttribute>().IsPerson) { string personEmail = string.Empty; if (property.GetCustomAttribute <IsPersonAttribute>().ReturnID) { if (((JContainer)listItem[listCoumnName])["results"] != null) { foreach (dynamic person in ((JContainer)listItem[listCoumnName])["results"]) { personEmail = personEmail.Trim(',') + "," + person["EMail"].ToString(); } } } else { if (((dynamic)listItem[listCoumnName]).EMail != null) { personEmail = ((dynamic)listItem[listCoumnName])["EMail"].ToString(); } } property.SetValue(tranItem, personEmail.Trim(',')); } else if (property.GetCustomAttribute <IsPersonAttribute>().ReturnName) { string personEmail = string.Empty; if (property.GetCustomAttribute <IsPersonAttribute>().IsMultiple) { if (((JContainer)listItem[listCoumnName])["results"] != null) { foreach (dynamic person in ((JContainer)listItem[listCoumnName])["results"]) { personEmail = personEmail.Trim(',') + "," + person["Title"].ToString(); } } } else { if (((dynamic)listItem[listCoumnName]).Title != null) { personEmail = ((dynamic)listItem[listCoumnName])["Title"].ToString(); } } property.SetValue(tranItem, personEmail.Trim(',')); } } else if (isFile) { if (((dynamic)((dynamic)listItem["AttachmentFiles"])).__deferred != null) { string url = ((dynamic)((dynamic)listItem["AttachmentFiles"])).__deferred.uri.Value; List <FileDetails> objAttachmentFiles = helper.GetAttachmentsUsingREST(url); property.SetValue(tranItem, objAttachmentFiles); } } else { object value = ((JValue)listItem[listCoumnName]).Value; Type t = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; object safeValue = (value == null) ? null : Convert.ChangeType(value, t); property.SetValue(tranItem, safeValue); } } else if (isTran) { if (getSubItems) { string listName = property.GetCustomAttribute <IsTranAttribute>().TranListName; Type tSubTran = property.GetCustomAttribute <IsTranAttribute>().TranType; if (!string.IsNullOrEmpty(listName)) { List <ITrans> subTrans = this.GetTransactionListData(context, web, tSubTran, listName, Convert.ToInt32(listItem["ID"])); property.SetValue(tranItem, subTrans); } } } } catch (Exception ex) { Logger.Error(string.Format("Error While Save tran Item - Message:{0}, StackTrace: {1}", ex.Message, ex.StackTrace)); } } return(tranItem); }
/// <summary> /// Sets the tran properties. /// </summary> /// <param name="context">The context.</param> /// <param name="web">The web.</param> /// <param name="listItem">The list item.</param> /// <param name="tranItem">The tran item.</param> /// <param name="properties">The properties.</param> /// <param name="getSubItems">if set to <c>true</c> [get sub items].</param> /// <returns> /// Transaction item /// </returns> private ITrans SetTranProperties(ClientContext context, Web web, ListItem listItem, ITrans tranItem, PropertyInfo[] properties, bool getSubItems = true) { foreach (PropertyInfo property in properties) { bool isListColumn = property.GetCustomAttribute <IsListColumnAttribute>() == null || property.GetCustomAttribute <IsListColumnAttribute>().IsListColumn; bool isTask = property.GetCustomAttribute <IsTaskAttribute>() != null && property.GetCustomAttribute <IsTaskAttribute>().IsTaskField; bool isTran = property.GetCustomAttribute <IsTranAttribute>() != null && property.GetCustomAttribute <IsTranAttribute>().IsTranField; string listCoumnName = property.GetCustomAttribute <FieldColumnNameAttribute>() != null && !string.IsNullOrEmpty(property.GetCustomAttribute <FieldColumnNameAttribute>().FieldsInformation) ? property.GetCustomAttribute <FieldColumnNameAttribute>().FieldsInformation : property.Name; bool isFile = property.GetCustomAttribute <IsFileAttribute>() != null && property.GetCustomAttribute <IsFileAttribute>().IsFile; if (isListColumn) { if (property.GetCustomAttribute <FieldColumnNameAttribute>() != null && property.GetCustomAttribute <FieldColumnNameAttribute>().IsLookup) { FieldLookupValue lookupField = listItem[listCoumnName] as FieldLookupValue; property.SetValue(tranItem, lookupField.LookupId); } else if (property.GetCustomAttribute <IsPersonAttribute>() != null && property.GetCustomAttribute <IsPersonAttribute>().IsPerson) { FieldUserValue[] users = null; if (listItem[listCoumnName] != null) { if (property.GetCustomAttribute <IsPersonAttribute>().IsMultiple) { users = listItem[listCoumnName] as FieldUserValue[]; } else { users = new FieldUserValue[1]; users[0] = listItem[listCoumnName] as FieldUserValue; } } if (users != null && property.GetCustomAttribute <IsPersonAttribute>().ReturnID) { string personEmails = GetUserIDsFromPersonField(context, web, users); property.SetValue(tranItem, personEmails); } else if (users != null && property.GetCustomAttribute <IsPersonAttribute>().ReturnName) { string personEmails = GetPersonValueFromPersonField(context, web, users, Person.Name); property.SetValue(tranItem, personEmails); } else if (users != null && property.GetCustomAttribute <IsPersonAttribute>().ReturnAlias) { string personEmails = GetPersonValueFromPersonField(context, web, users, Person.Alias); property.SetValue(tranItem, personEmails); } } else if (isFile) { if (Convert.ToString(listItem["Attachments"]) == "True") { context.Load(listItem.AttachmentFiles); context.ExecuteQuery(); AttachmentCollection attachments = listItem.AttachmentFiles; List <FileDetails> objAttachmentFiles = GetAttachments(attachments); property.SetValue(tranItem, objAttachmentFiles); } } else { object value = listItem[listCoumnName]; Type t = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; object safeValue = (value == null) ? null : Convert.ChangeType(value, t); if (t.Name.ToUpper().Trim() == Constants.DateTimeString.ToUpper().Trim() && safeValue != null) { var timeInfo = TimeZoneInfo.FindSystemTimeZoneById(Constants.TimeZoneName); safeValue = TimeZoneInfo.ConvertTimeFromUtc((DateTime)safeValue, timeInfo); } property.SetValue(tranItem, safeValue); } } else if (isTran) { if (getSubItems) { string listName = property.GetCustomAttribute <IsTranAttribute>().TranListName; Type tSubTran = property.GetCustomAttribute <IsTranAttribute>().TranType; if (!string.IsNullOrEmpty(listName)) { List <ITrans> subTrans = this.GetTransactionListData(context, web, tSubTran, listName, Convert.ToInt32(listItem["ID"])); property.SetValue(tranItem, subTrans); } } } ////else if (isTask) ////{ //// string listName = property.GetCustomAttribute<IsTaskAttribute>().TaskListName; //// Type tSubTask = property.GetCustomAttribute<IsTaskAttribute>().TaskType; //// if (!string.IsNullOrEmpty(listName)) //// { //// List<ITask> subTasks = this.GetTaskListData(context, web, tSubTask, listName, Convert.ToInt32(listItem["ID"])); //// property.SetValue(taskItem, subTasks); //// } ////} } return(tranItem); }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Cyan; Aviation aviation = new Aviation(); Cargo cargo = new Cargo(); Military military = new Military(); Pasanger pasanger = new Pasanger(); Ty134 ty134 = new Ty134(); Boing boing = new Boing(); cargo.addInfo(); Console.WriteLine('\n'); military.addInfo(); Console.WriteLine('\n'); pasanger.addInfo(); Console.WriteLine('\n'); Console.ForegroundColor = ConsoleColor.Magenta; aviation.Type(); cargo.Type(); military.Type(); pasanger.Type(); ty134.Type(); boing.Type(); Console.WriteLine('\n'); Console.ForegroundColor = ConsoleColor.White; bool fly = pasanger is Aviation; if (fly) { Aviation confOne = (Aviation)pasanger; confOne.Type(); } Console.ForegroundColor = ConsoleColor.Red; ITransport confTwo = military as ITransport; ITrans confThree = cargo as ITrans; if (confTwo != null) { confTwo.Info(); } if (confThree != null) { confThree.Info("fsfe"); } Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(cargo.ToString()); Console.ForegroundColor = ConsoleColor.Green; Transport obj = pasanger as Transport;//можно ли преобразовать Console.WriteLine(obj.GetType()); Console.ForegroundColor = ConsoleColor.Yellow; if (military is Transport)//преднадлежит { Console.WriteLine(true + "\n"); } Console.ForegroundColor = ConsoleColor.Magenta; Transport[] mas = { cargo, military, pasanger }; foreach (Transport x in mas) { Console.WriteLine(Printer.iAmPrinting(x)); } Console.ReadKey(); }