2016-06-23 2 views
1

절차 (수정을) 결과 :C 번호가 저장 조회하는 절차는

alter procedure searchProgramUnitResult(
    @id char(10) 
) 
as 
begin 
    select id from table1 where id = @id 
end 
DBML 디자이너에서

샘 절차 (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; 
} 
+1

스토어드 프로 시저 정의가 이상하게 보입니다. '@ id'는 _ 출력 _이지만 _input_과 같이 사용하고 있습니다 (아무 것도 설정하지 않은 것). –

+0

그냥 내 대답을 시도하고 내게 의견을주고,이 링크는 도움이 될 것입니다 2 http://stackoverflow.com/questions/4873607/how-to-use-dbcontext-database-sqlquerytelementsql-params-with-stored-proced –

답변

0

당신이 그런 식으로 호출 할 수 있습니다 :

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

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

코드 첫째로도 작동 다른 접근 방식을 :

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); 
} 

표가 당신의 엔티티/클래스 이름!

+0

this.Database.SqlQuery ("storedProcedureName", params); 이 모든 엔티티 유형의 목록을 반환합니다 –

+0

나는 SQL 서버 2005 후 당신이 exec를 사용할 필요가 없다고 생각합니다! –

+1

SQL Server 2008 R2의 관리 스튜디오를 사용하고 있으며 빌드 실행 옵션에'exec'이 있습니다 –

0

데이터 유형에 대해 질문합니까? 당신이 당신의 DbContext에 저장 프로 시저를 매핑 한 경우

public List<string> search (string id){ 
    DataContextClass db = new DataContextClass();   
    List<string> results = db.searchProgramUnit(id).ToList(); 
    return results; 
} 
+0

아니 테이블의 모든 coloumns는'string'입니다 –

+0

문자열 ID를 전달하고 다시 돌려줍니다. select 문에 모든 열을 나열하려면 엔티티 프레임 워크가 결과 개체를 만듭니다. db.searchProgramUnit (id) .ToList()는 속성이있는 객체의 컬렉션을 반환합니다. id = @id 인 table1의 select id는 실제 문제를 이해하는 데별로 도움이되지 않기 때문에 실제로 작업중인 저장 프로 시저를 게시하십시오. – Dennis