Example #1
0
        static void Main(string[] args)
        {
            string pathBase = AppDomain.CurrentDomain.BaseDirectory;

            string mdPath = pathBase;

            File.Delete(mdPath + "test.md");
            DataTable tables = ExecuteDataTable("select * from INFORMATION_SCHEMA.TABLES");

            foreach (DataRow tableName in tables.Rows)
            {
                string tablename = (string)tableName["TABLE_NAME"];
                //if (tablename.StartsWith("WX_"))
                //{
                List <string> Rows = new List <string>();
                Rows.Add("### " + tablename + "(*快来修改我*)");
                Rows.Add("");
                Rows.Add("| 列名           | 字段                       | 数据类型           | PK   | NULL  | DEFAULT      | 描述                                                                               |");
                Rows.Add("|----------------|----------------------------|--------------------|------|-------|--------------|------------------------------------------------------------------------------------|");
                DataTable tableColumns = ExecuteDataTable("select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=@tablename",
                                                          new SqlParameter("tablename", tablename));
                foreach (DataRow column in tableColumns.Rows)
                {
                    string column_name = (string)column["COLUMN_NAME"];
                    string data_type   = (string)column["DATA_TYPE"];
                    string character_maximum_length = column["CHARACTER_MAXIMUM_LENGTH"].ToString();
                    string column_default           = column["COLUMN_DEFAULT"].ToString();
                    string is_nullable = column["IS_NULLABLE"].ToString();

                    MDTableRow mdtablerow = new MDTableRow();
                    mdtablerow.column_name = column_name;
                    mdtablerow.data_type   = data_type;
                    mdtablerow.character_maximum_length = character_maximum_length;
                    mdtablerow.column_default           = column_default;
                    mdtablerow.is_nullable = is_nullable;

                    Rows.Add(WriteTableRow(mdtablerow));
                }
                //File.Delete(mdPath + "test.md");
                File.AppendAllLines(mdPath + "test.md", Rows);
                Console.WriteLine(tablename + " 生成完成");
                //}
            }
            Console.WriteLine();
            Console.WriteLine("生成结束!");
            Console.ReadKey();
        }
Example #2
0
        public static string WriteTableRow(MDTableRow mdtablerow)
        {
            string datatype = mdtablerow.character_maximum_length == "" || mdtablerow.character_maximum_length == "2147483647"
                ? "`" + mdtablerow.data_type.ToUpper() + "`" : "`" + mdtablerow.data_type.ToUpper() + "(" + mdtablerow.character_maximum_length + ")`";

            string data_tp = "`" + System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(SqlDataTypeToNetDataType(mdtablerow.data_type)) + "`";

            string str = string.Format("|{0}|{1}|{2}|{3}|{4}|{5}|{6}|"
                                       , Pad("", "----------------".Length, false)
                                       , Pad(mdtablerow.column_name, "----------------------------".Length)
                                       , Pad(mdtablerow.character_maximum_length == "-1" ? "`" + mdtablerow.data_type.ToUpper() + "(MAX)`" : datatype, "--------------------".Length)
                                       , Pad(mdtablerow.column_name == "ID" ? "PK" : "", "------".Length)
                                       , Pad(mdtablerow.is_nullable == "NO" ? "" : "NULL", "-------".Length)
                                       , Pad(mdtablerow.column_default, "--------------".Length)
                                       , Pad("", "------------------------------------------------------------------------------------".Length)
                                       );

            return(str);
        }