Exemplo n.º 1
0
        private static string ToIndex(string server, string db, string tb)
        {
            StringBuilder cBuilder = new StringBuilder();

            var tableDao = new GeneralDAO.TableDAO();
            var Indexs   = tableDao.GetIndexs(server, db, tb);

            cBuilder.AppendLine("--【索引】------------------------------------------");
            foreach (DBIndexInfo idx in Indexs)
            {
                if (idx.IsPrimaryKey)//忽略主键
                {
                    continue;
                }

                cBuilder.AppendLine("CREATE " + (idx.IsUnique ? " UNIQUE " : "") + idx.IndexDes + " INDEX " + idx.IndexName);
                cBuilder.AppendLine("ON [dbo]." + tb + " (" + idx.ColumnNames + ")");
                cBuilder.AppendLine("GO");
            }
            cBuilder.AppendLine("");
            return(cBuilder.ToString());
        }
Exemplo n.º 2
0
        private static string ToTable(string server, string db, DBTableInfo tb)
        {
            StringBuilder cBuilder = new StringBuilder();

            cBuilder.AppendLine("--【表结构】------------------------------------------");
            cBuilder.AppendLine("CREATE TABLE [dbo].[" + tb.TableName + "](");

            var idx           = 0;
            var tableDao      = new GeneralDAO.TableDAO();
            var Indexs        = tableDao.GetIndexs(server, db, tb.TableName);
            var primaryColumn = Indexs.FirstOrDefault(t => t.IsPrimaryKey);

            foreach (DBColInfo c in tb.Cols)
            {
                idx++;
                if (string.IsNullOrWhiteSpace(c.ColumnName))
                {
                    continue;
                }
                var scriptPrimaryKey = "";
                if (primaryColumn != null)
                {
                    scriptPrimaryKey = c.ColumnName == primaryColumn.ColumnNames ? "primary key" : "";
                }
                var scriptEnd      = idx == tb.Cols.Count ? "" : ",";
                var scriptNullable = c.IsNullable ? "null" : "not null";


                cBuilder.AppendLine(string.Format("   [{0}] [{1}]{2} {3} {4} {5} --{6}"
                                                  , c.ColumnName, c.ColumnType, c.ColumnLengthStr, scriptNullable, scriptPrimaryKey, scriptEnd, c.DisplayName));
            }
            cBuilder.AppendLine(")");
            cBuilder.AppendLine("GO");
            cBuilder.AppendLine("");
            return(cBuilder.ToString());
        }