コード例 #1
0
        private static Dictionary <string, EntityBase> ConvertTableXmlToEntityDic(string tableXmlPath)
        {
            Dictionary <string, EntityBase> entityDic = new Dictionary <string, EntityBase>();
            Schemas schemas = SerializeUtil.Deserialize <Schemas>(tableXmlPath);

            if (schemas != null && schemas.Tables.Count > 0)
            {
                foreach (var schema in schemas.Tables)
                {
                    if (!entityDic.ContainsKey(schema.Name))
                    {
                        EntityBase entityBase = new EntityBase {
                            EntityName = schema.Name
                        };
                        foreach (var attr in schema.Attributes)
                        {
                            EntityAttributeBase attribute = new EntityAttributeBase
                            {
                                AttributeName = attr.AttributeName,
                                AttributeType = attr.AttributeType,
                                AttributeSize = attr.AttributeSize,
                                IsPrimaryKey  = attr.IsPrimaryKey,
                                IsNotNull     = attr.IsNotNull
                            };
                            if (!entityBase.Attributes.ContainsKey(attr.AttributeName))
                            {
                                entityBase.Attributes.Add(attr.AttributeName, attribute);
                            }
                        }
                        entityDic.Add(schema.Name, entityBase);
                    }
                }
            }
            return(entityDic);
        }
コード例 #2
0
        private SqlParameter Convert <T>(EntityAttributeBase attributeBase, T data)
        {
            var          propValue = data.GetType().GetProperty(attributeBase.AttributeName).GetValue(data);
            SqlParameter parameter = new SqlParameter(attributeBase.AttributeName, propValue ?? DBNull.Value);

            switch (attributeBase.AttributeType)
            {
            case "nvarchar":
                parameter.SqlDbType = SqlDbType.NVarChar;
                break;

            case "varchar":
                parameter.SqlDbType = SqlDbType.VarChar;
                break;

            case "int":
                parameter.SqlDbType = SqlDbType.Int;
                break;

            case "bigint":
                parameter.SqlDbType = SqlDbType.BigInt;
                break;

            case "bit":
                parameter.SqlDbType = SqlDbType.Bit;
                break;

            case "float":
                parameter.SqlDbType = SqlDbType.Float;
                break;

            case "double":
                parameter.SqlDbType = SqlDbType.Decimal;
                break;

            case "ntext":
                parameter.SqlDbType = SqlDbType.NText;
                break;
            }
            if (!string.IsNullOrEmpty(attributeBase.AttributeSize) && int.Parse(attributeBase.AttributeSize) > 0)
            {
                parameter.Size = int.Parse(attributeBase.AttributeSize);
            }
            return(parameter);
        }
コード例 #3
0
        public void CreateField(string entityName, EntityAttributeBase attributeBase, string connectionName = null)
        {
            string sqlText = "ALTER TABLE {0} ADD {1} {2}";
            string temp    = string.Format(sqlText, entityName, attributeBase.AttributeName, attributeBase.AttributeType);

            if (!string.IsNullOrEmpty(attributeBase.AttributeSize) && int.Parse(attributeBase.AttributeSize) > 0)
            {
                temp += string.Format("({0})", attributeBase.AttributeSize);
            }
            if (attributeBase.IsPrimaryKey)
            {
                temp += " PRIMARY KEY";
            }
            if (attributeBase.IsNotNull && !attributeBase.IsPrimaryKey)
            {
                temp += " NOT NULL";
            }
            SqlServerBaseOrder.ExecuteNonQuery(SqlServerConnectionProvider.Instance.GetConnectionString(connectionName), temp);
        }
コード例 #4
0
        public int InsertIntoEntityAttributeBase(EntityAttributeBase attributeBase, string connectionName = null)
        {
            string sqlText = "INSERT INTO EntityAttributeBase(Id, EntityId, AttributeName, AttributeType, AttributeSize, IsPrimaryKey, IsNotNull)VALUES(@Id, @EntityId, @AttributeName, @AttributeType, @AttributeSize, @IsPrimaryKey, @IsNotNull)";

            SqlParameter[] paras = new SqlParameter[] {
                new SqlParameter("@Id", SqlDbType.NVarChar, 256),
                new SqlParameter("@EntityId", SqlDbType.NVarChar, 256),
                new SqlParameter("@AttributeName", SqlDbType.NVarChar, 256),
                new SqlParameter("@AttributeType", SqlDbType.NVarChar, 256),
                new SqlParameter("@AttributeSize", SqlDbType.NVarChar, 256),
                new SqlParameter("@IsPrimaryKey", SqlDbType.Bit),
                new SqlParameter("@IsNotNull", SqlDbType.Bit)
            };
            paras[0].Value = attributeBase.Id;
            paras[1].Value = attributeBase.EntityId;
            paras[2].Value = attributeBase.AttributeName;
            paras[3].Value = attributeBase.AttributeType;
            paras[4].Value = string.IsNullOrEmpty(attributeBase.AttributeSize) ? (object)DBNull.Value : attributeBase.AttributeSize;
            paras[5].Value = attributeBase.IsPrimaryKey;
            paras[6].Value = attributeBase.IsNotNull;
            string connStr = SqlServerConnectionProvider.Instance.GetConnectionString(connectionName);

            return(SqlServerBaseOrder.ExecuteNonQuery(connStr, sqlText, paras));
        }