开发者

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

开发者 https://www.devze.com 2022-12-20 00:19 出处:网络
I have a stored procedure query that I want to return the details of columns within a speci开发者_开发知识库fic table.When I run the sp in Sql server managements studio I get results returned.The prob

I have a stored procedure query that I want to return the details of columns within a speci开发者_开发知识库fic table. When I run the sp in Sql server managements studio I get results returned. The problem is when I try to execute the query from C# code. 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.

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

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. The use of the SYSOBJECTS references is not guaranteed to be the same between SQL Versions.

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'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号