简体   繁体   English

如何从存储过程返回的表中获取列名

[英]How to get column names from table returned from stored procedure

I have a stored procedure which returns a table. 我有一个返回表的存储过程。 The stored proc is called via a linq datacontext. 存储的proc通过linq datacontext调用。

It works fine and I get the table back however I really want to also get the title relating to each particular cell returned. 它工作正常,我得到了表,但我真的想得到与返回的每个特定单元格相关的标题。

Does anyone know how to do this? 有谁知道如何做到这一点?

The stored procedure call is like: 存储过程调用如下:

var table = DataContext.GetTable().ToList();

So I get a List<GetTable> . 所以我得到了一个List<GetTable> The data is fine I just want the column names as well. 数据很好我只想要列名。

You can use reflection to do this 您可以使用反射来执行此操作

var columns = table.First();

var properties = (from property in columns.GetType().GetProperties()
                  select property.Name).ToList();

foreach (var property in properties)
    Console.WriteLine(property);

You can also use the Meta Model in the System.Data.Linq.Mapping Namespace 您还可以在System.Data.Linq.Mapping命名空间中使用元模型

AttributeMappingSource mappping = new System.Data.Linq.Mapping.AttributeMappingSource();
var model = mappping.GetModel(typeof(MyDataContext));


MetaFunction function = model.GetFunction(typeof(MyDataContext).GetMethod("MyStoredProc"));


foreach (var resultTypes in function.ResultRowTypes)
{

    foreach (var column in resultTypes.DataMembers)
    Console.WriteLine(column.Name);

}

Since Stored procedures can have more than one result set this is probably the better way since it handles that case. 由于存储过程可以有多个结果集,因此它可能是处理该情况的更好方法。

You can try using reflection on the entity types. 您可以尝试在实体类型上使用反射。 As far as I can tell, all generated properties correspond to the columns in the table if they have an associated ColumnAttribute . 据我所知,如果它们具有关联的ColumnAttribute ,则所有生成的属性都对应于表中的列。 You can try this: 你可以试试这个:

public static List<string> GetColumnNames<TEntity>(Table<TEntity> table)
    where TEntity : class
{
    return GetColumnNames(typeof(TEntity));
}

public static List<string> GetColumnNames(DataContext context, string functionName)
{
    var retType = context.GetType().GetMethod(functionName).ReturnType;
    System.Diagnostics.Debug.Assert(retType.Name == "ISingleResult`1");
    return GetColumnNames(retType.GetGenericArguments().Single());
}

public static List<string> GetColumnNames(Type entityType)
{
    return (from p in entityType.GetProperties()
            let columnAttribute = p.GetCustomAttributes(false)
                                   .OfType<System.Data.Linq.Mapping.ColumnAttribute>()
                                   .SingleOrDefault()
            where columnAttribute != null
            select columnAttribute.Name ?? p.Name)
           .ToList();
}

// usage:
// from a function/procedure name
var names1 = GetColumnNames(DataContext, "GetTable");
// or by entity type directly (the return type of the function/procedure)
var names2 = GetColumnNames(typeof(GetTable));

In light of seeing Conrad's use of the meta model, I came up with this. 鉴于看到康拉德使用元模型,我想出了这个。 Associations (added by LINQ to SQL) would need to be filtered out to get the column names from the table. 需要过滤掉关联(由LINQ to SQL添加)以从表中获取列名。

public static List<string> GetColumnNames<TEntity>(Table<TEntity> table)
    where TEntity : class
{
    return new System.Data.Linq.Mapping.AttributeMappingSource()
        .GetModel(table.Context.GetType())
        .GetTable(typeof(TEntity))
        .RowType
        .DataMembers
        .Where(dm => !dm.IsAssociation)
        .Select(dm => dm.MappedName)
        .ToList();
}

public static List<string> GetColumnNamesMeta(DataContext context, string functionName)
{
    var type = context.GetType();
    return new System.Data.Linq.Mapping.AttributeMappingSource()
        .GetModel(type)
        .GetFunction(type.GetMethod(functionName))
        .ResultRowTypes
        .SelectMany(rrt => rrt.DataMembers
                              .Where(dm => !dm.IsAssociation)
                              .Select(dm => dm.MappedName))
        .ToList();
}

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

相关问题 如何使DataSet从存储过程中识别表名? - How to get the DataSet to recognize the table names from stored procedure? 如何从C#调用存储过程,并获取返回值? - How to call stored procedure from C#, and get the returned value? 如何从存储过程返回的datetime对象获取日期? - How to get date from datetime object returned by stored procedure? 如何检查存储过程返回的NULL? - how to check for NULL returned from stored procedure? 如何在 Visual Studio 的存储过程中使用查询来使用另一列中的值从表中获取 ID - How to use query in a stored procedure in Visual Studio to get the ID from a table using the value in another column 如何让C#Query组件识别从sql存储过程中的临时表返回的数据列 - How do I get the C# Query component to recognise columns returned data from a temporary table in a sql stored procedure ASP.NET对LINQ中存储过程返回的列求和 - ASP.NET Sum a column returned from stored procedure in LINQ 从SQL端的存储过程更改返回的表名 - Change returned table name from stored procedure at the SQL side 如何处理从LINQ to SQL中的存储过程返回的NULLable Int列 - How can I handle a NULLable Int column returned from a stored procedure in LINQ to SQL 如何从C#中的SQL Server存储过程获取返回的结果 - How to get the result returned back from a SQL Server stored procedure in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM