private static int DmlProcessing(DMLType dmlType, int?id, string title, string making_time, string serves, string ingredients, int cost) { using (SqlConnection connection = new SqlConnection(DB.ConnectionString)) { using (SqlCommand command = connection.CreateCommand()) { connection.Open(); if (dmlType == DMLType.Delete || dmlType == DMLType.Update) { command.Parameters.Add(new SqlParameter("id", (int)id)); } if (dmlType == DMLType.Insert || dmlType == DMLType.Update) { command.Parameters.Add(new SqlParameter("title", title)); command.Parameters.Add(new SqlParameter("making_time", making_time)); command.Parameters.Add(new SqlParameter("serves", serves)); command.Parameters.Add(new SqlParameter("ingredients", ingredients)); command.Parameters.Add(new SqlParameter("cost", cost)); command.Parameters.Add(new SqlParameter("updated_at", DateTime.Now)); command.Parameters.Add(new SqlParameter("created_at", DateTime.Now)); } // SQLの準備 if (dmlType == DMLType.Insert) { command.CommandText = @"INSERT INTO dbo.recipes (title, making_time,serves,ingredients,cost,updated_at, created_at) OUTPUT INSERTED.id VALUES (@title, @making_time, @serves,@ingredients,@cost,@updated_at,@created_at)"; var aa = command.ExecuteScalar(); return((int)aa); } else if (dmlType == DMLType.Delete) { command.CommandText = @"DELETE FROM dbo.recipes where id = @id"; } else { command.CommandText = @"Update dbo.recipes Set title = @title, making_time = @making_time, serves = @serves, ingredients = @ingredients, cost = @cost, updated_at = @updated_at where id = @id"; } // SQLの実行 return(command.ExecuteNonQuery()); } } }
public Constructor <T> Update(T entity) { _DMLType = DMLType.UPDATE; this.entity = entity; this.property = from t in this.entitymap.PropertyMaps where t.PrimaryKey == null && t.Ignore == false select t; this.format = new Func <IEnumerable <IPropertyMap>, string>(BuildUpdate); return(this); }
public Constructor <T> Insert_Return_Id(T entity) { _DMLType = DMLType.INSERT; this.entity = entity; this.property = from t in this.entitymap.PropertyMaps where t.PrimaryKey == null && t.Ignore == false select t; this.format = new Func <IEnumerable <IPropertyMap>, string>(BuildInsert_Return_Id); return(this); }
public Constructor <T> Update(T entity, params string[] arrs) { _DMLType = DMLType.UPDATE; var partmap = from t in this.entitymap.PropertyMaps where arrs.Contains(t.Name) select t; this.entity = entity; this.property = from t in partmap where t.PrimaryKey == null && t.Ignore == false select t; this.format = new Func <IEnumerable <IPropertyMap>, string>(BuildUpdate); return(this); }
public Constructor <T> Delete <K>(IEnumerable <K> pkvlues) { _DMLType = DMLType.DELETE; string pkName = this.entitymap.PropertyMaps.First(m => m.PrimaryKey != null)?.Name; if (typeof(K).Name == typeof(string).Name) { var pk = from t in pkvlues select $"'{t}'"; _where = new Where($"{pkName} in ({string.Join(",", pk)})"); } if (typeof(K).Name == typeof(int).Name) { _where = new Where($"{pkName} in ({string.Join(",", pkvlues)})"); } this.format = null; return(this); }
public Constructor <T> Delete() { _DMLType = DMLType.DELETE; this.format = null; return(this); }