コード例 #1
0
        public void CreateTable(string tableName, List <DbColumnInfo> columns, bool isCreatePrimaryKey = true)
        {
            var lstSql = new List <string>();

            //注意,ORACLE创建自增加主键,要先创建一个序列,为每个表都单独创建一个序列 序列名称为 SET_大写表名
            StringBuilder builder = new StringBuilder($" create table {tableName} (");

            foreach (var column in columns)
            {
                builder.Append(string.Format("{0} {1}{2} {3} {4} {5},",
                                             column.ColumnName,
                                             column.DataType,
                                             Utils.IsCharColumn(column.DataType) && column.Length > 0 ? $"({column.Length})" : "",
                                             column.IsNullable && !column.IsPrimarykey ? "null" : "not null",
                                             //column.IsIdentity ? "identity" : "",
                                             column.IsPrimarykey ? "primary key" : "",
                                             !string.IsNullOrEmpty(column.DefaultValue) ? $"default {column.DefaultValue}" : ""
                                             ));
            }
            lstSql.Add(builder.ToString().TrimEnd(',') + ") ");

            //是否有自增加列
            var identityColumn = columns.Where(p => p.IsIdentity).FirstOrDefault();

            if (identityColumn != null)
            {
                //生成一个SEQ_表名大写 的序列 如 SEQ_CMS_USER,在insert数据时用到
                lstSql.Add($" create sequence SEQ_{tableName.ToUpper()} minvalue 1 maxvalue 99999999999 start with 1 increment by 1 nocache ");
            }

            dbAccess.ExecTrans(lstSql);
        }