public static void DeleteItem(string tableName, List <WHERE> where) { string sql = "DELETE FROM " + TABLEPREFIX + tableName + " WHERE 1=1"; Hashtable param = new Hashtable(); foreach (WHERE w in where) { sql += " AND " + w.COLUMNNAME + w.OPERATOR + "@" + w.COLUMNNAME; param.Add(w.COLUMNNAME, w.VALUE); } SQLMs.RunSQL(sql, param); }
public static void InsertEntity <T>(string tableName, T entity) { string sql = string.Empty; Type item = typeof(T); Hashtable param = new Hashtable(); //var storeItem = DALBase.ctx.MetadataWorkspace.GetItems<EntityType>(DataSpace.OSpace).Where(et => et.FullName == entity.ToString()).FirstOrDefault(); EntityConnection con = new EntityConnection("name=MotionSteelEntities"); MetadataWorkspace vStorageWorkspace = con.GetMetadataWorkspace(); EdmType vStorageEdmType = vStorageWorkspace.GetType(tableName, "MotionSteelModel.Store", DataSpace.SSpace); EntityType storeItem = vStorageEdmType as EntityType; sql = "INSERT INTO " + TABLEPREFIX + tableName + "("; string values = " VALUES("; bool hasIdentity = false; string identityColName = string.Empty; foreach (EdmMember member in storeItem.Members) { if (member.TypeUsage.EdmType.BaseType != null) {// gercek alanlari yapsin, kendi koydugu navigation propertyleri yapmasin diye PropertyInfo info = item.GetProperty(member.Name); Facet identity = member.TypeUsage.Facets.Where(p => p.Name == "StoreGeneratedPattern").FirstOrDefault(); if (storeItem.KeyMembers.Where(p => p.Name == member.Name).Count() > 0) {// key var if (identity != null && (StoreGeneratedPattern)identity.Value == StoreGeneratedPattern.Identity) { hasIdentity = true; identityColName = member.Name; } else { // identity degilse genede ekleyecegiz,ornek related product tablosu icin sql += "[" + member.Name + "],"; values += "@" + member.Name + ","; param.Add(member.Name, info.GetValue(entity, null)); } } else { if (identity != null && (StoreGeneratedPattern)identity.Value == StoreGeneratedPattern.Computed) {// bir sey yapmasin } else if (identity != null && (StoreGeneratedPattern)identity.Value == StoreGeneratedPattern.Identity) {// key kolon haricinde Identity var hasIdentity = true; identityColName = member.Name; } else { sql += "[" + member.Name + "],"; values += "@" + member.Name + ","; param.Add(member.Name, info.GetValue(entity, null)); } } } } sql = sql.TrimEnd(',') + ") " + values.TrimEnd(',') + ")"; if (hasIdentity) {//varsa alip ID ye setlemek lazim, ornek, orderi ekleyince sonrasinda order detail icin kullanilacak if (DBTYPE == SqlType.MSSql) { sql += Environment.NewLine + " SELECT SCOPE_IDENTITY()"; } else if (DBTYPE == SqlType.MySql) { sql += ";" + Environment.NewLine + " SELECT LAST_INSERT_ID(); "; } object id = null; id = SQLMs.RunScalar(sql, param); PropertyInfo info = item.GetProperty(identityColName); id = Convert.ChangeType(id, info.PropertyType); info.SetValue(entity, id, null); } else { SQLMs.RunSQL(sql, param); } }