概述Program.cs using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.IO;using System.Text;namespace BuildMd{ class Program { stat Program.cs
using System;using System.Collections.Generic;using System.Data;using System.Data.sqlClIEnt;using System.IO;using System.Text;namespace BuildMd{ class Program { 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 + string.Format("{0}.md",tablename["table_CATALOG"]),Rows); Console.Writeline(tablename + " 生成完成"); //} } Console.Writeline(); Console.Writeline("生成结束!"); Console.ReadKey(); } 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),"------------------------------------------------------------------------------------".Length) ); return str; } public static string Pad(string basestr,int padlength,bool isCN = false) { int baselength = (int)(basestr.Length * (isCN ? 2 : 1)); int length = padlength - baselength; return basestr.Padleft(2).padright(padlength - (isCN ? basestr.Length : 0)); } public static Datatable ExecuteDatatable(string cmdText,params sqlParameter[] parameters) { using (sqlConnection conn = new sqlConnection(@"server=WZY-PC;database=HibernateBlog;uID=sa;pwd=xxxxxx;")) { conn.open(); using (sqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); Datatable dt = new Datatable(); sqlDataAdapter adapter = new sqlDataAdapter(cmd); adapter.Fill(dt); return dt; } } } private static string sqlDataTypetoNetDataType(string datatype) { switch (datatype) { case "int": return "int"; case "nvarchar": case "varchar": case "nchar": case "char": return "string"; case "bit": return "bool"; case "datetime": case "datetime2": return "DateTime"; case "decimal": return "decimal"; default: return "object"; } } } class MDtableRow { public string column_name { get; set; } public string data_type { get; set; } public string character_maximum_length { get; set; } public string column_default { get; set; } public string is_nullable { get; set; } }}
运行结果如图:
UsersBlogs(
快来修改)
列名 | 字段 | 数据类型 | PK | NulL | DEFAulT | 描述 |
---|
| UserID | @H_599_403@INT@H_308_404@ | | | | |
| BlogID | @H_599_403@INT@H_308_404@ | | | | |
Blogs(
快来修改)
列名 | 字段 | 数据类型 | PK | NulL | DEFAulT | 描述 |
---|
| ID | @H_599_403@INT@H_308_404@ | | | | |
| Title | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
| SubTitle | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
| AllowsComments | @H_599_403@BIT@H_308_404@ | | NulL | | |
| CreatedAt | @H_599_403@DATETIME@H_308_404@ | | NulL | | |
CategorIEs(
快来修改)
列名 | 字段 | 数据类型 | PK | NulL | DEFAulT | 描述 |
---|
| ID | @H_599_403@INT@H_308_404@ | | | | |
| name | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
CategorIEsposts(
快来修改)
列名 | 字段 | 数据类型 | PK | NulL | DEFAulT | 描述 |
---|
| categoryID | @H_599_403@INT@H_308_404@ | | | | |
| PostID | @H_599_403@INT@H_308_404@ | | | | |
Comments(
快来修改)
列名 | 字段 | 数据类型 | PK | NulL | DEFAulT | 描述 |
---|
| ID | @H_599_403@INT@H_308_404@ | | | | |
| name | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
| Email | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
| HomePage | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
| Ip | @H_599_403@INT@H_308_404@ | | NulL | | |
| Text | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
| PostID | @H_599_403@INT@H_308_404@ | | NulL | | |
posts(
快来修改)
列名 | 字段 | 数据类型 | PK | NulL | DEFAulT | 描述 |
---|
| ID | @H_599_403@INT@H_308_404@ | | | | |
| Title | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
| Text | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
| PostedAt | @H_599_403@DATETIME@H_308_404@ | | NulL | | |
| BlogID | @H_599_403@INT@H_308_404@ | | NulL | | |
| UserID | @H_599_403@INT@H_308_404@ | | NulL | | |
Tags(
快来修改)
列名 | 字段 | 数据类型 | PK | NulL | DEFAulT | 描述 |
---|
| ID | @H_599_403@INT@H_308_404@ | | | | |
| name | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
| ItemID | @H_599_403@INT@H_308_404@ | | NulL | | |
| ItemType | @H_599_403@INT@H_308_404@ | | NulL | | |
sysdiagrams(
快来修改)
列名 | 字段 | 数据类型 | PK | NulL | DEFAulT | 描述 |
---|
| name | @H_599_403@NVARCHAR(128)@H_308_404@ | | | | |
| principal_ID | @H_599_403@INT@H_308_404@ | | | | |
| diagram_ID | @H_599_403@INT@H_308_404@ | | | | |
| version | @H_599_403@INT@H_308_404@ | | NulL | | |
| deFinition | @H_599_403@VARBINARY(MAX)@H_308_404@ | | NulL | | |
Users(
快来修改)
列名 | 字段 | 数据类型 | PK | NulL | DEFAulT | 描述 |
---|
| ID | @H_599_403@INT@H_308_404@ | | | | |
| Password | @H_599_403@VARBINARY(8000)@H_308_404@ | | NulL | | |
| Username | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
| Email | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL | | |
| CreatedAt | @H_599_403@DATETIME@H_308_404@ | | NulL | | |
| Bio | @H_599_403@NVARCHAR(255)@H_308_404@ | | NulL |
总结
以上是内存溢出为你收集整理的将SqlServer表转换为Markdown全部内容,希望文章能够帮你解决将SqlServer表转换为Markdown所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)