示例#1
0
        public void Render(StringBuilder sb)
        {
            var sqlTypeName = SqlTypeMapper.GetSqlTypeName(m_targetType, 0);

            if (string.IsNullOrWhiteSpace(sqlTypeName))
            {
                m_operand.Render(sb);
                return;
            }

            sb.Append("CAST(");
            m_operand.Render(sb);
            sb.Append(" AS ");
            sb.Append(sqlTypeName);
            sb.Append(")");
        }
        public void CreateTable(string tableName, string pkColumnName, Type pkColumnType, bool pkAutogenerated)
        {
            var identity   = string.Empty;
            var pkTypeName = SqlTypeMapper.GetSqlTypeName(pkColumnType, 0);

            if (pkAutogenerated)
            {
                identity = "IDENTITY(1,1) ";
            }

            m_tablesBuilder.AppendLine($"IF NOT EXISTS(SELECT TOP 1 1 FROM sys.tables WHERE name = '{tableName}')");
            m_tablesBuilder.AppendLine("BEGIN");
            m_tablesBuilder.Append("\t");
            m_tablesBuilder.AppendLine($"CREATE TABLE [{tableName}] ([{pkColumnName}] {pkTypeName} {identity}NOT NULL, ");
            m_tablesBuilder.AppendLine(
                $"\tCONSTRAINT [PK_{tableName}] PRIMARY KEY CLUSTERED ([{pkColumnName}] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]");
            m_tablesBuilder.AppendLine("\t);");
            m_tablesBuilder.AppendLine("END");
            m_tablesBuilder.AppendLine();
        }
示例#3
0
        protected override object InsertEntity(IEntity entity, ITransaction <SqlConnection> transaction)
        {
            var values = entity.GetValues().Where(v => !v.IsPk).ToList();

            var columnsList = string.Join(", ", values.Select(i => $"[{i.ColumnName}]"));
            var paramList   = string.Join(", ", values.Select(i => $"@{i.ColumnName}"));

            var sql =
                $"INSERT INTO [{entity.DbEntityName}] ({columnsList}) VALUES ({paramList}); SELECT CAST(SCOPE_IDENTITY() AS {SqlTypeMapper.GetSqlTypeName(entity.PrimaryKeyType, 0)});";

            using (var command = new SqlCommand(sql, transaction.GetConnection()))
            {
                foreach (var entityColumnValue in values)
                {
                    command.Parameters.AddWithValue($"@{entityColumnValue.ColumnName}", entityColumnValue.Value ?? DBNull.Value);
                }

                var newPk = command.ExecuteScalar();
                entity.PrimaryKeyValue = newPk;

                return(newPk);
            }
        }