public static string GetEntityTable <T>() { string tName = BaseTO.GetTableName <T>(); Type type = typeof(T); tName = tName.Replace("_" + type.Namespace + "_" + type.Name, ""); return(tName); }
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(T t, string pkValue) { Query query = new Query(); string tableName = BaseTO.GetTableName <T>(); query.Selects = GetEntityExtendQuery(); string[] primaryKeys = t.GetPrimaryKey <T>(); if (null == primaryKeys || 0 == primaryKeys.Length) { throw new ExceptionNoPrimaryKey(); } if (1 < primaryKeys.Length) { //此方法只用于实体类只能有一个主键的情况 throw new ExceptionErrorPrimaryKeyNumber(); } query.Wheres.AddEqualPair(primaryKeys[0], pkValue); return(GetItem(query)); }
//通过多个主键查询 public T GetItemByPK(BaseTO to) { Query query = new Query(); string tableName = BaseTO.GetTableName <T>(); query.Selects = GetEntityExtendQuery(); string[] primaryKeys = to.GetPrimaryKey <T>(); if (null == primaryKeys || 0 == primaryKeys.Length) { throw new ExceptionNoPrimaryKey(); } foreach (string pkName in primaryKeys) { query.Wheres.AddSql(string.Format("{0} = @{0}", pkName), new string[] { "@" + pkName }); query.Parameters.Add("@" + pkName, new Query.ParameterItem() { Type = SqlDbType.NVarChar, Value = to.GetPropertyValue(pkName).ToString() }); //query.Wheres.AddEqualPair(pkName, to.GetPropertyValue(pkName).ToString()); } 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)); }
public BaseDAO(string connectionName) { _tableName = BaseTO.GetTableName <T>(); _connectionName = connectionName; }
public BaseDAO() { _tableName = BaseTO.GetTableName <T>(); _connectionName = SqlServerSet.ConnectionName; }
public static Query.SelectItemCollection GetEntityExtendQuery() { string tableName = BaseTO.GetTableName <T>(); return(entityExtendQuery.ThreadSafeRead <string, Query.SelectItemCollection>(tableName, "entityExtendQuery")); }
public static void SetEntityExtendQuery(Query.SelectItemCollection extQuerys) { string tableName = BaseTO.GetTableName <T>(); entityExtendQuery.ThreadSafeWrite <string, Query.SelectItemCollection>(tableName, extQuerys, "entityExtendQuery"); }