public static string CreateSqlToSelect <TEntity>(IFilter filter, params string[] orderbys) where TEntity : new() { IHashEntity entity = (IHashEntity) new TEntity(); string sql = "SELECT "; foreach (string colName in entity.ColNames) { sql = sql + colName + ","; } sql = sql.Remove(sql.Length - 1, 1) + " FROM {0}"; sql = System.String.Format(sql, entity.TableName); if (filter != null) { sql = System.String.Format(sql + " where {0}", filter.GetExpression()); } if (orderbys.Count() > 0) { string orderbyStr = ""; foreach (string orderby in orderbys) { orderbyStr = orderbyStr + orderby + ","; } sql = sql + " order by " + orderbyStr.Remove(orderbyStr.Length - 1, 1); } return(sql); }
public void Insert(IHashEntity entity, bool PkeyIsIdentity) { SqlFactory sql = new SqlFactory(entity.TableName, SqlType.INSERT); Hashtable htalbe = entity.PropertySet; foreach (string colName in entity.ColNames) { if (!PkeyIsIdentity || colName != entity.PKeyName) { if (htalbe[colName] != null) { switch (SqlFactory.GetColType(htalbe[colName].GetType())) { case 1: sql.AppendQuote(colName, htalbe[colName]); break; case 2: sql.Append(colName, htalbe[colName]); break; default: sql.Append(colName, htalbe[colName]); break; } } } } ExecuteNonQuery(sql.CreateSQL()); }
public FastDBService(IHashEntity _HashEntity, IListEntity _ListEntity, ISingleEntity _SingleEntity, ITreeEntity _TreeEntity) { HashEntity = _HashEntity; ListEntity = _ListEntity; SingleEntity = _SingleEntity; TreeEntity = _TreeEntity; }
public void Delete <TEntity>(IFilter filter) where TEntity : new() { IHashEntity entity = (IHashEntity) new TEntity(); StringBuilder strSql = new StringBuilder(); strSql.AppendFormat("DELETE * FROM [{0}] ", entity.TableName); if (filter != null) { strSql.AppendFormat(" where {0}", filter.GetExpression()); } ExecuteNonQuery(strSql.ToString()); }
public static string CreateSqlToUpdate(IHashEntity entity) { Hashtable htalbe = entity.PropertySet; if (htalbe[entity.PKeyName] == null) { throw new Exception("实体没有主键不能用此方法更新"); } SqlFactory sql = new SqlFactory(entity.TableName, SqlType.UPDATE); foreach (string colName in entity.ColNames) { if (colName != entity.PKeyName && htalbe[colName] != System.DBNull.Value && htalbe[colName] != null) { switch (SqlFactory.GetColType(htalbe[colName].GetType())) { case 1: sql.AppendQuote(colName, htalbe[colName]); break; case 2: sql.Append(colName, htalbe[colName]); break; default: sql.Append(colName, htalbe[colName]); break; } } } switch (SqlFactory.GetColType(htalbe[entity.PKeyName].GetType())) { case 1: sql.AppendWhereQuote(entity.PKeyName, htalbe[entity.PKeyName]); break; case 2: sql.AppendWhere(entity.PKeyName, htalbe[entity.PKeyName]); break; default: sql.AppendWhere(entity.PKeyName, htalbe[entity.PKeyName]); break; } return(sql.CreateSQL()); }
public void Delete(IHashEntity entity) { string sql; switch (SqlFactory.GetColType(entity.PropertySet[entity.PKeyName].GetType())) { case 1: sql = "DELETE * FROM [{0}] WHERE {1} = '{2}'"; break; case 2: sql = "DELETE * FROM [{0}] WHERE {1} = {2}"; break; default: sql = "DELETE * FROM [{0}] WHERE {1} = '{2}'"; break; } ExecuteNonQuery(string.Format(sql, entity.TableName, entity.PKeyName, entity.PropertySet[entity.PKeyName])); }
public void Update(IHashEntity entity, IFilter filter) { Hashtable htalbe = entity.PropertySet; StringBuilder strSql = new StringBuilder(); strSql.AppendFormat("UPDATE {0} SET ", entity.TableName); foreach (string colName in entity.ColNames) { if (colName != entity.PKeyName && htalbe[colName] != System.DBNull.Value && htalbe[colName] != null) { switch (SqlFactory.GetColType(htalbe[colName].GetType())) { case 1: strSql.AppendFormat("{0}='{1}',", colName, htalbe[colName]); break; case 2: strSql.AppendFormat("{0}={1},", colName, htalbe[colName]); break; default: strSql.AppendFormat("{0}='{1}',", colName, htalbe[colName]); break; } } } strSql.Remove(strSql.Length - 1, 1); if (filter != null) { strSql.AppendFormat(" where {0}", filter.GetExpression()); } ExecuteNonQuery(strSql.ToString()); }
public void Update <TEntity, TKey>(TEntity entity) { IHashEntity ihe = (IHashEntity)entity; DataRabbit.IEntity <TKey> ien = (DataRabbit.IEntity <TKey>)entity; string SQL = String.Format("UPDATE [{0}] SET ", ihe.TableName); Hashtable htalbe = ihe.PropertySet; string KeyName = ""; int i = 0; foreach (string colName in htalbe.Keys) { if (!htalbe[colName].Equals(ien.GetPKeyValue())) { SQL = SQL + String.Format("[{0}] = '{1}'", colName, htalbe[colName]); if (i < htalbe.Count - 2) { SQL = SQL + " , "; } i++; } else { KeyName = colName; } } if (KeyName != "") { SQL = SQL + String.Format("WHERE [{0}] = '{1}'", KeyName, ien.GetPKeyValue()); } else { throw new Exception("未设置主建,不能用此方法更新对像!请检查!"); } ExecuteNonQuery(SQL); }
public void Update(IHashEntity entity) { string sql = SqlFactory.CreateSqlToUpdate(entity); ExecuteNonQuery(sql); }