/// <summary> /// 获取实体(异步方式) /// </summary> /// <param name="strWhere">参数化查询条件(例如: and Name = @Name )</param> /// <param name="dict">参数的名/值集合</param> /// <returns></returns> public virtual async Task <U_GuestBookEntity> GetEntityAsync(string strWhere, Dictionary <string, object> dict = null) { U_GuestBookEntity obj = null; string strSQL = "select top 1 * from U_GuestBook where 1=1 " + strWhere; using (NullableDataReader reader = await Task.Run(() => _DB.GetDataReader(strSQL, dict))) { if (reader.Read()) { obj = GetEntityFromrdr(reader); } } return(obj); }
/// <summary> /// 把实体类转换成键/值对集合 /// </summary> /// <param name="entity"></param> /// <param name="dict"></param> private static void GetParameters(U_GuestBookEntity entity, Dictionary <string, object> dict) { dict.Add("ID", entity.ID); dict.Add("GuestName", entity.GuestName); dict.Add("GuestOicq", entity.GuestOicq); dict.Add("GuestMsn", entity.GuestMsn); dict.Add("GuestEmail", entity.GuestEmail); dict.Add("GuestContent", entity.GuestContent); dict.Add("GuestIsPrivate", entity.GuestIsPrivate); dict.Add("GuestFace", entity.GuestFace); dict.Add("GuestHomepage", entity.GuestHomepage); dict.Add("GuestImages", entity.GuestImages); dict.Add("AdminReply", entity.AdminReply); dict.Add("AdminReplyTime", entity.AdminReplyTime); }
/// <summary> /// 通过数据读取器生成实体类 /// </summary> /// <param name="rdr"></param> /// <returns></returns> private static U_GuestBookEntity GetEntityFromrdr(NullableDataReader rdr) { U_GuestBookEntity info = new U_GuestBookEntity(); info.ID = rdr.GetInt32("ID"); info.GuestName = rdr.GetString("GuestName"); info.GuestOicq = rdr.GetString("GuestOicq"); info.GuestMsn = rdr.GetString("GuestMsn"); info.GuestEmail = rdr.GetString("GuestEmail"); info.GuestContent = rdr.GetString("GuestContent"); info.GuestIsPrivate = rdr.GetBoolean("GuestIsPrivate"); info.GuestFace = rdr.GetString("GuestFace"); info.GuestHomepage = rdr.GetString("GuestHomepage"); info.GuestImages = rdr.GetString("GuestImages"); info.AdminReply = rdr.GetString("AdminReply"); info.AdminReplyTime = rdr.GetNullableDateTime("AdminReplyTime"); return(info); }
/// <summary> /// 增加一条记录,返回新的ID号。需要有一个单一主键,并且开启有标识符属性(异步方式) /// </summary> /// <param name="entity">实体模型</param> /// <returns></returns> public virtual async Task <int> InsertAsync(U_GuestBookEntity entity) { if (entity.ID <= 0) { entity.ID = GetNewID(); } Dictionary <string, object> dict = new Dictionary <string, object>(); GetParameters(entity, dict); string strSQL = "insert into U_GuestBook (" + "ID," + "GuestName," + "GuestOicq," + "GuestMsn," + "GuestEmail," + "GuestContent," + "GuestIsPrivate," + "GuestFace," + "GuestHomepage," + "GuestImages," + "AdminReply," + "AdminReplyTime) " + "values(" + "@ID," + "@GuestName," + "@GuestOicq," + "@GuestMsn," + "@GuestEmail," + "@GuestContent," + "@GuestIsPrivate," + "@GuestFace," + "@GuestHomepage," + "@GuestImages," + "@AdminReply," + "@AdminReplyTime)"; if (await Task.Run(() => _DB.ExeSQLResult(strSQL, dict))) { return(DataConverter.CLng(entity.ID)); } return(-1); }
/// <summary> /// 增加一条记录 /// </summary> /// <param name="entity">实体模型</param> /// <returns></returns> public virtual bool Add(U_GuestBookEntity entity) { if (entity.ID <= 0) { entity.ID = GetNewID(); } Dictionary <string, object> dict = new Dictionary <string, object>(); GetParameters(entity, dict); string strSQL = "insert into U_GuestBook (" + "ID," + "GuestName," + "GuestOicq," + "GuestMsn," + "GuestEmail," + "GuestContent," + "GuestIsPrivate," + "GuestFace," + "GuestHomepage," + "GuestImages," + "AdminReply," + "AdminReplyTime) " + "values(" + "@ID," + "@GuestName," + "@GuestOicq," + "@GuestMsn," + "@GuestEmail," + "@GuestContent," + "@GuestIsPrivate," + "@GuestFace," + "@GuestHomepage," + "@GuestImages," + "@AdminReply," + "@AdminReplyTime)"; return(_DB.ExeSQLResult(strSQL, dict)); }
/// <summary> /// 更新一条记录(异步方式) /// </summary> /// <param name="entity">实体模型</param> /// <returns></returns> public virtual async Task <bool> UpdateAsync(U_GuestBookEntity entity) { Dictionary <string, object> dict = new Dictionary <string, object>(); GetParameters(entity, dict); string strSQL = "Update U_GuestBook SET " + "GuestName = @GuestName," + "GuestOicq = @GuestOicq," + "GuestMsn = @GuestMsn," + "GuestEmail = @GuestEmail," + "GuestContent = @GuestContent," + "GuestIsPrivate = @GuestIsPrivate," + "GuestFace = @GuestFace," + "GuestHomepage = @GuestHomepage," + "GuestImages = @GuestImages," + "AdminReply = @AdminReply," + "AdminReplyTime = @AdminReplyTime" + " WHERE " + "ID = @ID"; return(await Task.Run(() => _DB.ExeSQLResult(strSQL, dict))); }
/// <summary> /// 增加或更新一条记录(异步方式) /// </summary> /// <param name="entity">实体模型</param> /// <param name="IsSave">是否增加</param> /// <returns></returns> public virtual async Task <bool> AddOrUpdateAsync(U_GuestBookEntity entity, bool IsSave) { return(IsSave ? await AddAsync(entity) : await UpdateAsync(entity)); }
/// <summary> /// 增加或更新一条记录 /// </summary> /// <param name="entity">实体模型</param> /// <param name="IsSave">是否增加</param> /// <returns></returns> public virtual bool AddOrUpdate(U_GuestBookEntity entity, bool IsSave) { return(IsSave ? Add(entity) : Update(entity)); }