示例#1
0
        public override int Delete <T>(T obj)
        {
            MySqlServerMapper                    mapper           = new MySqlServerMapper();
            string                               tableName        = mapper.GetTableName <T>();
            List <PrimaryKeyAttribute>           primaryKeys      = mapper.GetPrimaryKeys <T>();
            Dictionary <ColumnAttribute, object> listColumnValues = mapper.GetColumnValues <T>(obj);

            string query = string.Empty;

            foreach (PrimaryKeyAttribute primaryKey in primaryKeys)
            {
                //tìm cot khóa chính
                ColumnAttribute column = mapper.FindColumn(primaryKey.Name, listColumnValues);
                if (column != null)
                {
                    string format = "{0} = {1}, ";
                    if (column.Type == DataType.NCHAR || column.Type == DataType.NVARCHAR)
                    {
                        format = "{0} = N'{1}', ";
                    }
                    else if (column.Type == DataType.CHAR || column.Type == DataType.VARCHAR)
                    {
                        format = "{0} = '{1}', ";
                    }

                    query += string.Format(format, primaryKey.Name, listColumnValues[column]);
                }
            }
            if (!string.IsNullOrEmpty(query))
            {
                query = query.Substring(0, query.Length - 2);
                query = string.Format("DELETE {0} WHERE {1}", tableName, query);
            }
            return(ExecuteNonQuery(query));
        }
示例#2
0
        public override int Update <T>(T obj)
        {
            MySqlServerMapper mapper = new MySqlServerMapper();

            string tableName = mapper.GetTableName <T>();
            List <PrimaryKeyAttribute>           primaryKeys      = mapper.GetPrimaryKeys <T>();
            Dictionary <ColumnAttribute, object> listColumnValues = mapper.GetColumnValues <T>(obj);
            string query = string.Empty;

            if (listColumnValues != null && primaryKeys != null)
            {
                string setStr = string.Empty;

                foreach (ColumnAttribute column in listColumnValues.Keys)
                {
                    string format = "{0} = {1}, ";
                    if (column.Type == DataType.NCHAR || column.Type == DataType.NVARCHAR)
                    {
                        format = "{0} = N'{1}', ";
                    }
                    else if (column.Type == DataType.CHAR || column.Type == DataType.VARCHAR)
                    {
                        format = "{0} = '{1}', ";
                    }

                    setStr += string.Format(format, column.Name, listColumnValues[column]);
                }
                if (!string.IsNullOrEmpty(setStr))
                {
                    setStr = setStr.Substring(0, setStr.Length - 2);
                }

                foreach (PrimaryKeyAttribute primaryKey in primaryKeys)
                {
                    ColumnAttribute column = mapper.FindColumn(primaryKey.Name, listColumnValues);
                    if (column != null)
                    {
                        string format = "{0} = {1}, ";
                        if (column.Type == DataType.NCHAR || column.Type == DataType.NVARCHAR)
                        {
                            format = "{0} = N'{1}', ";
                        }
                        else if (column.Type == DataType.CHAR || column.Type == DataType.VARCHAR)
                        {
                            format = "{0} = '{1}', ";
                        }

                        query += string.Format(format, primaryKey.Name, listColumnValues[column]);
                    }
                }
                if (!string.IsNullOrEmpty(query))
                {
                    query = query.Substring(0, query.Length - 2);
                    query = string.Format("UPDATE {0} SET {1} WHERE {2}", tableName, setStr, query);
                }
            }
            return(ExecuteNonQuery(query));
        }