惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
G
Google Developers Blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
V
Visual Studio Blog
博客园 - Franky
S
SegmentFault 最新的问题
Jina AI
Jina AI
爱范儿
爱范儿
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
C
Check Point Blog
月光博客
月光博客
P
Proofpoint News Feed
T
The Blog of Author Tim Ferriss
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Martin Fowler
Martin Fowler

博客园 - 车神

怎么在本地进行域名解析 解决sql server数据库还原的时候出现一直显示还原中 密码正则表达式大全 UNIQUE约束添加规则 Linux配置nginx开机自启 netcore跨平台之 Linux部署nginx代理webapi 为.net Core WebApi 创建Linux守护进程 Linux-CentOS 部署 dotnetCore API /前端 VUE mongodb:常用命令大全 SqlServer数据类型、C#SqlDbType对应关系及转换 vue+nginx编译部署 vue.js安装过程(npm安装) JS将/Date(1446704778000)/转换成str javascript工具函数 如何提高ASP.NET页面载入速度的方法 SQL Server遍历表的几种方法 SQL2005语句实现行转列,列转行 sql中获取周、月、季度、年的第一天与最后一天 sql时间函数
C# 如何获取SQL Server 中指定数据表的所有字段名和字段类型
车神 · 2019-01-08 · via 博客园 - 车神

如何获取指定数据表的所有字段名和字段类型。SqlConnection.GetSchema方法有2个重载形式,获取指定数据表的所有字段名和字段类型的秘密就在GetSchema (String, String[])的第二个参数中。

定义如下:

public override DataTable GetSchema(
    string collectionName,
    string[] restrictionValues
)

参数collectionName指定要返回的架构的名称,取值为静态类 SqlClientMetaDataCollectionNames的成员,如果要取列信息,则取值为SqlClientMetaDataCollectionNames.Columns。

关于SqlClientMetaDataCollectionNames类成员的详细信息参见:https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlclientmetadatacollectionnames_fields(v=vs.100).aspx

参数restrictionValues为请求架构的一组限制值,对于不同的架构集类型,有不同的写法。要具体了解,可以调用GetSchema("Restrictions") 方法。

针对SQL Server 数据库,restrictionValues的长度为4,其中restrictionValues[0]为Catalog(数据库名),restrictionValues[1]为Owner(所有者),restrictionValues[2]为Table(表名),restrictionValues[3]为Column(列名)。

我们要查询某张表中的列名等信息,则可以通过设置restrictionValues[2]="SomeTableName"来实现。

实现代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data;
 4 using System.Data.SqlClient;
 5 
 6 namespace scratchline.cn
 7 {
 8     public struct Field
 9     {
10         public string Name;
11         public string Type;
12     }
13 
14     public class scratchline
15     {
16         public List<Field> GetFileds(string connectionString, string tableName)
17         {
18             List<Field> _Fields = new List<Field>();
19             SqlConnection _Connection = new SqlConnection(connectionString);
20             try
21             {
22                 _Connection.Open();
23 
24                 string[] restrictionValues = new string[4];
25                 restrictionValues[0] = null; // Catalog
26                 restrictionValues[1] = null; // Owner
27                 restrictionValues[2] = tableName; // Table
28                 restrictionValues[3] = null; // Column
29 
30                 using (DataTable dt = _Connection.GetSchema(SqlClientMetaDataCollectionNames.Columns, restrictionValues))
31                 {
32                     foreach (DataRow dr in dt.Rows)
33                     {
34                         Field field;
35                         field.Name = dr["column_name"].ToString();
36                         field.Type = dr["data_type"].ToString();
37                         _Fields.Add(field);
38                     }
39                 }
40             }
41             catch (Exception ex)
42             {
43                 throw ex;
44             }
45             finally
46             {
47                 _Connection.Dispose();
48             }
49 
50             return _Fields;
51         }
52     }
53 }

View Code

总结:SqlConnection.GetSchema方法用于获取数据库架构信息,通过不同参数的组合可实现各种数据架构信息的获取功能。