简体   繁体   English

查询表列名不返回任何结果给调用代码

[英]Query for table column names returns no results to the calling code

I have a stored procedure query that I want to return the details of columns within a specific table. 我有一个存储过程查询,我想返回特定表中列的详细信息。 When I run the sp in Sql server managements studio I get results returned. 当我在Sql Server Managements Studio中运行sp时,返回结果。 The problem is when I try to execute the query from C# code. 问题是当我尝试从C#代码执行查询时。 There are no exceptions thrown and the relevant persmissions are granted to execute the procedure in the database but no results are returned to the code. 没有引发任何异常,并且授予了相关权限以执行数据库中的过程,但是没有结果返回给代码。 I'm using the enterprise application block version 3.1. 我正在使用企业应用程序块3.1版。

This is my query 这是我的查询

    SELECT   SysObjects.[Name] as TableName,
             SysColumns.[Name] as ColumnName,
             SysTypes.[Name] As DataType,
             SysColumns.[Length] As Length
    FROM    SysObjects INNER JOIN SysColumns   ON SysObjects.[Id] = SysColumns.[Id]
    INNER JOIN SysTypes  ON SysTypes.[xtype] = SysColumns.[xtype]
    WHERE  SysObjects.[type] = 'U'
        AND SysObjects.[Name] = 'MyTableName'
    ORDER BY  SysObjects.[Name]

C# Calling code C#调用代码

using (DbCommand dbCommand = db.GetStoredProcCommand("StoredProcedureName"))
{
    DataSet data = new DataSet();

    db.LoadDataSet(dbCommand, data, "MyTableName");
    if (data.Tables.Count > 0 && data.Tables[0].Rows.Count > 0)
    {
        // Do stuff with the returned data
    }
}

I'm not sure about the C# issue, but you should consider changing your query to use the data-abstraction layer provided by Microsoft. 我不确定C#问题,但您应该考虑更改查询以使用Microsoft提供的数据抽象层。 The use of the SYSOBJECTS references is not guaranteed to be the same between SQL Versions. 不能保证SYSOBJECTS引用的使用在SQL版本之间是相同的。

Instead I'd use this: 相反,我会用这个:

--
-- for a specific tables columns
--
select * 
from INFORMATION_SCHEMA.COLUMNS

--
-- For all the user tables in the current database..
--
select * 
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='MyTableName'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM