public SqlDeleteQuery(SqlConnection cnn, string connectionString, T obj) : base(cnn, connectionString)
        {
            SqlMapper mapper    = new SqlMapper();
            string    tableName = mapper.GetTableName <T>();
            List <PrimaryKeyAttribute>           primaryKeys      = mapper.GetPrimaryKeys <T>();
            Dictionary <ColumnAttribute, object> listColumnValues = mapper.GetColumnValues <T>(obj);

            string whereStr = string.Empty;

            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}', ";
                    }

                    whereStr += string.Format(format, primaryKey.Name, listColumnValues[column]);
                }
            }
            if (!string.IsNullOrEmpty(whereStr))
            {
                whereStr = whereStr.Substring(0, whereStr.Length - 2);
                _query   = string.Format("DELETE {0} WHERE {1}", tableName, whereStr);
            }
        }
Exemple #2
0
        public SqlInsertQuery(SqlConnection cnn, string connectionString, T obj) : base(cnn, connectionString)
        {
            SqlMapper mapper = new SqlMapper();

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

            if (listColumnNameValues.Count != 0)
            {
                string columnStr = string.Empty;
                string valueStr  = string.Empty;

                foreach (ColumnAttribute column in listColumnNameValues.Keys)
                {
                    bool isAutoID = false;
                    foreach (PrimaryKeyAttribute primaryKey in primaryKeys)
                    {
                        if (column.Name == primaryKey.Name && primaryKey.AutoID)
                        {
                            isAutoID = true;
                            break;
                        }
                    }

                    if (!isAutoID)
                    {
                        string format = "{0}, ";
                        if (column.Type == DataType.NCHAR || column.Type == DataType.NVARCHAR)
                        {
                            format = "N'{0}', ";
                        }
                        else if (column.Type == DataType.CHAR || column.Type == DataType.VARCHAR)
                        {
                            format = "'{0}', ";
                        }
                        columnStr += string.Format("{0}, ", column.Name);
                        valueStr  += string.Format(format, listColumnNameValues[column]);
                    }
                }
                if (!string.IsNullOrEmpty(columnStr) && !string.IsNullOrEmpty(valueStr))
                {
                    columnStr = columnStr.Substring(0, columnStr.Length - 2);
                    valueStr  = valueStr.Substring(0, valueStr.Length - 2);
                    _query    = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", tableName, columnStr, valueStr);
                }
            }
        }