// return 1 if success, 0 if failed public int UpdateOne <U, TU>(Dictionary <string, U> key, TU item, bool isInTransaction, string raiseOrDecrease, string table) { int af = 0; // if the item isn't the same type with schema, return 0 if (typeof(TU) != item.GetType()) { Console.WriteLine("The input item and the model isn't the same type"); return(af); } List <string> listFields = FieldsHandle.FilterField(item); string updateString = SqlStringBuilder.GetUpdateString(table, key, listFields, raiseOrDecrease); bool conOpened = con.State == ConnectionState.Open; if (!conOpened) { conOpened = OpenCon(); } if (conOpened) { mySqlCommand.CommandText = updateString; mySqlCommand.Parameters.Clear(); if (isInTransaction) { mySqlCommand.Transaction = tran; } else { mySqlCommand.Transaction = null; } HqlCommand cmd = new HqlCommand(mySqlCommand); cmd.AddValue(listFields, item); string keyFields = new List <string>(key.Keys)[0]; cmd.AddValue(keyFields, key[keyFields]); af = cmd.ExecuteNonQuery(); if (!isInTransaction) { CloseCon(); } } return(af); }
public int InsertOne <TU>(TU item, bool isInTransaction, string table) where TU : class, new() { int af = 0; // if item and schema isn't the same type, return 0 if (typeof(TU) != item.GetType()) { Console.WriteLine("The input item and the model isn't the same type"); return(af); } List <string> listField = FieldsHandle.FilterField(item); // gen the insert string string insertString = SqlStringBuilder.GetInsertString(table, listField); // check if the connection is opened bool conOpened = con.State == ConnectionState.Open; // if not, open it if (!conOpened) { conOpened = OpenCon(); } if (conOpened) { mySqlCommand.CommandText = insertString; mySqlCommand.Parameters.Clear(); if (isInTransaction) { mySqlCommand.Transaction = tran; } else { mySqlCommand.Transaction = null; } HqlCommand cmd = new HqlCommand(mySqlCommand); cmd.AddValue(listField, item); af = cmd.ExecuteNonQuery(); // if the param willCloseCon == true, close the connection if (!isInTransaction) { CloseCon(); } } return(af); }