本文介绍了如何在 SQL Server 查询中显示表结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SELECT DateTime, Skill, Name, TimeZone, ID, User, Employee, Leader 
FROM t_Agent_Skill_Group_Half_Hour AS t

我需要在查询中查看表结构.

I need to view the table structure in a query.

推荐答案

对于 SQL Server,如果使用较新的版本,可以使用

For SQL Server, if using a newer version, you can use

select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='tableName'

有多种获取模式的方法.使用 ADO.NET,您可以使用 模式方法.使用 DbConnectionGetSchema 方法DataReaderGetSchemaTable 方法.

There are different ways to get the schema. Using ADO.NET, you can use the schema methods. Use the DbConnection's GetSchema method or the DataReader'sGetSchemaTable method.

如果您有一个用于查询的阅读器,您可以执行以下操作:

Provided that you have a reader for the for the query, you can do something like this:

using(DbCommand cmd = ...)
using(var reader = cmd.ExecuteReader())
{
    var schema = reader.GetSchemaTable();
    foreach(DataRow row in schema.Rows)
    {
        Debug.WriteLine(row["ColumnName"] + " - " + row["DataTypeName"])
    }
}

有关详细信息,请参阅本文.

See this article for further details.

这篇关于如何在 SQL Server 查询中显示表结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 11:14