public int DeleteByPKArray(long[] idArrar) { StringBuilder sb = new StringBuilder(); sb.Append("Delete from ").Append(_tableName); string[] primaryKeys = BaseTO.GetSQLPrimaryKey(BaseTO.GetTableName <T>()); if (null == primaryKeys || 0 == primaryKeys.Length) { throw new ExceptionNoPrimaryKey(); } if (1 < primaryKeys.Length) { //此方法只用于实体类只有一个主键的情况 throw new ExceptionErrorPrimaryKeyNumber(); } string idList = string.Join(",", idArrar); sb.Append(" where ") .Append(primaryKeys[0]) .Append(" in (") .Append(idList) .Append(")"); List <SqlParameter> parameters = new List <SqlParameter>(); SqlTransConn transConn = GetConnection(_connectionName); int result = 0; try { result = SqlHelper.ExecuteNonQuery(transConn, CommandType.Text, sb.ToString(), null); } catch (Exception ex) { throw new Exception(ex.Message + ":" + sb.ToString(), ex); } finally { if (!transConn.IsInTransaction()) { transConn.Close(); } } return(result); }
public int DeleteByPK(long id) { StringBuilder sb = new StringBuilder(); sb.Append("Delete from ").Append(_tableName); string[] primaryKeys = BaseTO.GetSQLPrimaryKey(BaseTO.GetTableName <T>()); if (null == primaryKeys || 0 == primaryKeys.Length) { throw new ExceptionNoPrimaryKey(); } if (1 < primaryKeys.Length) { //此方法只用于实体类只能有一个主键的情况 throw new ExceptionErrorPrimaryKeyNumber(); } sb.Append(" where ").Append(primaryKeys[0]).Append("=@PKValue"); List <SqlParameter> parameters = new List <SqlParameter>(); SqlParameter parameter = new SqlParameter("@PKValue", SqlDbType.Int, 0); parameter.Value = id; parameters.Add(parameter); SqlTransConn transConn = GetConnection(_connectionName); int result = 0; try { result = SqlHelper.ExecuteNonQuery(transConn, CommandType.Text, sb.ToString(), parameters); } catch (Exception ex) { throw new Exception(ex.Message + ":" + sb.ToString(), ex); } finally { if (!transConn.IsInTransaction()) { transConn.Close(); } } return(result); }
public T GetItemByPK(long id) { Query query = new Query(); string tableName = BaseTO.GetTableName <T>(); query.Selects = GetEntityExtendQuery(); string[] primaryKeys = BaseTO.GetSQLPrimaryKey(tableName); if (null == primaryKeys || 0 == primaryKeys.Length) { throw new ExceptionNoPrimaryKey(); } if (1 < primaryKeys.Length) { //此方法只用于实体类只能有一个主键的情况 throw new ExceptionErrorPrimaryKeyNumber(); } query.Wheres.AddEqualPair(primaryKeys[0], id); return(GetItem(query)); }
/// <summary> /// 根据多个主键ID查询 /// </summary> /// <param name="ids"></param> /// <returns></returns> public List <T> GetItemByPK(long[] ids) { Query query = new Query(); string tableName = BaseTO.GetTableName <T>(); query.Selects = GetEntityExtendQuery(); string[] primaryKeys = BaseTO.GetSQLPrimaryKey(tableName); if (null == primaryKeys || 0 == primaryKeys.Length) { throw new ExceptionNoPrimaryKey(); } if (1 < primaryKeys.Length) { //此方法只用于实体类只能有一个主键的情况 throw new ExceptionErrorPrimaryKeyNumber(); } if (ids == null || ids.Length < 1) { ids = new long[] { 0 }; } query.Wheres.AddSql(primaryKeys[0] + " in (" + string.Join(",", ids) + ")"); //query.Wheres.AddEqualPair(primaryKeys[0], id); return(GetItems(query)); }