2014-05-15 4 views
0

SQL Server 2008 R2에 두 개의 사용자 정의 스칼라 반환 함수 (예 : 아래에 예시 된 두 가지)를 배포해야합니다. 둘 다 자체적으로 호출 할 수 있습니다. 첫 번째 매개 변수는 두 번째 매개 변수에서도 호출 할 수 있습니다.호출 스택을 통한 하나의 SQL 컨텍스트 연결

[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)] 
public static bool Function1(string arg1) 
{ 
    using (SqlConnection sqlCnn = new SqlConnection("context connection=true")) 
    { 
     //... Some code here. 
    } 
    return true; 
} 

[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)] 
public static bool Function2(string arg1) 
{ 
    using (SqlConnection sqlCnn = new SqlConnection("context connection=true")) 
    { 
     bool ret1 = Function1("arg1"); 
     //... Some code here. 
    } 
    return true; 
} 

성공적으로 배포되었습니다. 이 기능 2를 호출한다면 다음과 같은 오류가 발생합니다 :

  1. :

질문

은 "System.InvalidOperationException 문맥 연결이 이미 사용 입니다" 무엇이 왜 같은 데이터베이스에서 데이터를 가져 오는 다른 함수에서 데이터베이스의 데이터를 가져 오는 하나의 함수를 호출하는 최상의 솔루션입니까? (정규 연결, Acc는 ~를 좋아합니다. SqlCommand 또는 다른 것을 사용하여 mon SQL Server 기능).

  • 신뢰할 때에 프로퍼티가 꺼져 있습니다, 그래서 일반 연결을 열 수 없습니다. 이 사실을 고려한 최선의 해결책은 무엇입니까?

  • 답변

    0

    코드에 오타가 있습니까? Function2를 호출하는 무한 재귀가있는 것처럼 보입니다. 내 대답은 Function1을 호출해야한다고 가정합니다.

    나는 Microsoft.SqlServer.Server.SqlFunction 너무 익숙하지 해요,하지만 당신은 다음과 같이 무언가를 함수를 오버로드 할 수 있는지를 도움이 될 것이다.

    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)] 
    public static bool Function1(string arg1) 
    { 
        using (SqlConnection sqlCnn = new SqlConnection("context connection=true")) 
        { 
         return Function1(arg1, sqlCnn); 
        } 
        return true; 
    } 
    
    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)] 
    public static bool Function1(string arg1, SqlConnection sqlCnn) 
    { 
        //... Some code here using sqlCnn which we won't close or dispose. 
        // as it's up to the caller to do that 
        return true; 
    } 
    
    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)] 
    public static bool Function2(string arg1) 
    { 
        using (SqlConnection sqlCnn = new SqlConnection("context connection=true")) 
        { 
         bool ret1 = Function1(arg1, sqlCnn); 
         //... Some code here. 
        } 
        return true; 
    } 
    

    때문에 기능 1은 기능 1 (문자열)를 사용할 수 있으며이 생성됩니다 호출이 아무것도 자신의 연결,하지만 기능 2 이는 기능 1 다음 같은 연결을 사용 (문자열,도록 SqlConnection)를 호출합니다.

    0

    SqlFunctionFunctionFunction2과 같은 메서드는 실제로 SQL Server에 대한 코드 인터페이스이며 서로 의존해서는 안됩니다. 그렇다면 코드가 간단한 정적 메서드에 비해 너무 복잡해지기 시작한다는 신호입니다. 당신이 많은 등의 기능이있는 경우

    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)] 
    public static bool Function1(string arg1) 
    { 
        using (SqlConnection sqlCnn = new SqlConnection("context connection=true")) 
        { 
         InnerFunction1(sqlCnn,arg1); 
        } 
        return true; 
    } 
    
    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)] 
    public static bool Function2(string arg1) 
    { 
        using (SqlConnection sqlCnn = new SqlConnection("context connection=true")) 
        { 
         InnerFunction2(sqlCnn,"arg1"); 
        } 
        return true; 
    } 
    
    private static bool InnerFunction1(SqlConnection sqlCnn,string arg1) 
    { 
         //... Some code here. 
    } 
    
    private static bool InnerFunction2(SqlConnection sqlCnn,string arg1) 
    { 
         bool ret1 = InnerFunction1(sqlCnn,"arg1"); 
         //... Some code here.   
    } 
    

    , 당신은 다른 클래스 구현을 추출 할 수 있습니다 :

    더 나은 솔루션은 SqlFunction 방법에 연결 관리를 떠나 다른 방법의 실제 구현을 넣어, 예를 들어하는 것 해당하는 경우에만 정적 클래스를 유지하여 SqlFunction 멤버를 노출시킵니다.

    관련 문제