通过Linq来获取数据库中的所有表及其字段名称

static void Main( string[] args)

        {

            DataClasses1DataContext dc = new DataClasses1DataContext();

            Console.WriteLine("Database: {0}", dc.Mapping.DatabaseName);//数据库名称

            Console.WriteLine("The table count is {0}",dc.Mapping.GetTables().Count());//表的数目

            var tables = from t in dc.Mapping.GetTables()

                         orderby t.TableName

                         select t;

 

           

            foreach (var table in tables)

            {

 

                Console.WriteLine("Tablename is :"+table.TableName);//表名称

                Console.WriteLine("The column count is :{0}", table.RowType.DataMembers.Count);//字段数目

                foreach (var col in table.RowType.DataMembers)

                {

                    Console.WriteLine(""t{0}"t{1}", col.MappedName, col.DbType);//字段名称及字段类型

                }

            }

            Console.WriteLine("This is the end.");

            Console.ReadLine();

        }

你可能感兴趣的:(LINQ)