查询与C#方法检索数据库元数据 [英] Query vs. C# method to retrieve database metadata

查看:96
本文介绍了查询与C#方法检索数据库元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为数据库中的每个表检索以下信息:

I need to retrieve for each table in the database the following info:

  1. 所有列名称
  2. 每列的类型
  3. 输入最大长度

执行此操作的可能方法是运行查询(甚至可以使用await(即异步)执行查询):

The possible way to do that is to run a query (even can execute it using await, i.e. async):

select object_NAME(c.object_id), c.name, t.name, c.max_length
from sys.columns c
INNER JOIN sys.types t
    ON t.system_type_id = c.system_type_id

另一方面,在连接上有GetSchema方法,使方法相同:

On the other hand there is GetSchema method on connection which makes the same:

DataTable columns = connection.GetSchema(SqlClientMetaDataCollectionNames.Columns, restrictions);
foreach (DataRow row in columns.Rows)
{
    string columnName = row[3].ToString();
    string columnDataType = row[7].ToString();   
    string columnDataTypeLen = row[8].ToString();
}

哪种方法更好用?看起来第二个应该更快-是吗? 性能如何?

Which of the methods is better to use? Looks that second one should be faster - am I right? What about performance?

推荐答案

使用任一方法并缓存结果

Use either method and cache the result

在正常操作下元数据不会更改,因此性能不是问题

Meta data doesn't change under normal operation so performance is a non-issue

这篇关于查询与C#方法检索数据库元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆