Ejemplo n.º 1
0
        /// <summary>
        /// Updates row in table.
        /// </summary>
        /// <param name="dbObject">Table name based on type name of object.</param>
        /// <returns></returns>
        public bool UpdateData(IDbObject dbObject, string condition = null)
        {
            GenericPropertyFinder <IDbObject> property = new GenericPropertyFinder <IDbObject>();

            IEnumerable <List <string> > obj = property.PrintTModelPropertyAndValue(dbObject);

            string table = dbObject.GetType().ToString().Substring(dbObject.GetType().ToString().LastIndexOf('.') + 1);
            string query = new DbQueryBuilder(obj, table, condition).BuildQuery(QueryType.Update);

            if (condition != null)
            {
                if (query.EndsWith(';'))
                {
                    query = query[0..^ 1];
Ejemplo n.º 2
0
        /// <summary>
        /// Insert data in database. Name of table based on argument type.
        /// </summary>
        /// <param name="dbObject">Table name based on type name of object.</param>
        /// <returns>Boolean, True if operation successful</returns>
        public bool InsertData(IDbObject dbObject)
        {
            GenericPropertyFinder <IDbObject> property = new GenericPropertyFinder <IDbObject>();

            IEnumerable <List <string> > obj = property.PrintTModelPropertyAndValue(dbObject);

            GetTableName(dbObject);
            string query = new DbQueryBuilder(obj, _table).BuildQuery(QueryType.Insert);

            try
            {
                using SqlConnection conn = new SqlConnection(ConnHelper.ConnStr(connection_name));
                using SqlCommand command = new SqlCommand(query, conn);
                conn.Open();
                command.ExecuteNonQuery();
                command.Dispose();
                conn.Close();

                return(true);
            }
            catch (SqlException e)
            {
                MessageBox.Show(
                    $"Provjerite vezu sa bazom podataka.\n {e.Message}",
                    "Greška",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);

                return(false);
            }
            catch (Exception e)
            {
                MessageBox.Show(
                    $"Nepoznata greška kod brisanja podataka, kontaktirajte podršku.\n {e.Message}",
                    "Greška",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);

                return(false);
            }
        }