C#检索存储过程结果

时间:2016-06-23 16:09:29

标签: c# entity-framework stored-procedures

程序(修改):

alter procedure searchProgramUnitResult(
    @id char(10)
)
as
begin
    select id from table1 where id = @id
end

DBML Designer中的Sam过程(在将过程导入MVC项目之后):

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.searchProgramUnit")]
public ISingleResult<searchProgramUnitResult> searchProgramUnit([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="VarChar(10)")] ref string id){
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())),id);
id = ((string)(result.GetParameterValue(0)));
return ((ISingleResult<searchProgramUnitResult>)(result.ReturnValue));
}

问题是,如何在另一个C#类中检索结果集?

public ??Data-type search (string id){
    DataContextClass db = new DataContextClass();         
    ??Datatype results = db.searchProgramUnit(id);
    return results;
}

2 个答案:

答案 0 :(得分:0)

您在询问数据类型吗?

An ELF executable had more than one PT_INTERP segment (i.e., tried to name more than one interpreter)

答案 1 :(得分:0)

如果您已在DbContext中映射存储过程,则可以这样调用它:

using (var context = new DataContextClass())
{
    var courses = context.searchProgramUnit("1");

    foreach (table1 cs in table1s)
        Console.WriteLine(cs.Name);
}

另一种与Code First一起使用的方法:

using (var ctx = new DataContextClass())
{
    var idParam = new SqlParameter
    {
        ParameterName = "id",
        Value = "1"
    };
    var table1List = ctx.Database.SqlQuery<table1>("exec searchProgramUnitResult @id ", idParam).ToList<table1>();

    foreach (table cs in table1List)
        Console.WriteLine("Name: {0}", cs.Name);
}

table1是你的实体/类名!