2012-10-19 5 views
1

테이블 열을 구문 분석하고 다른 테이블에 결과를 쓰는 CLR 함수를 작성했습니다. 기본 요구 사항은 Time 부분과 ID 부분을 포함하는 Detail 열을 구문 분석하는 것입니다. 결과는 두 ID 간의 시간 차이가됩니다.SQL CLR 함수 호출 오류

예 : Time1, Id1, Time2, Id2, Time3, Id3 ... 등 Time2-Time1은 Id1이 초 단위로 찍은 시간입니다.

일반 콘솔 응용 프로그램에서 같은 기능이 작동하지만 SQL Server에서 CLR 기능을 호출하면 예외가 throw됩니다. System.Security.HostProtectionException : CLR은 호스트에 의해 금지 된 작업을 수행 할 시도를 오류가

닷넷 프레임 워크 오류가 사용자 정의 루틴 또는 집계 "기능 1"의 실행 중에 발생했습니다. (완전 신뢰에서만 사용 가능) 보호 된 자원했다 : 모든 요구되는 자원과 같다 : UI System.Security.HostProtectionException : UserDefinedFunctions.Function1 (문자열 MSISDN, 문자열 PROMPTDETAILS)에서

내 코드는 다음과 같습니다 SqlConnection conn = 새 SqlConnection(); SqlCommand cmd = 새 SqlCommand();

int count = 0; 
    string PromptPart = string.Empty; 
    string PrevPromptPart = string.Empty; 
    DateTime TimePart; 
    TimePart = new DateTime(); 
    DateTime PrevTimePart; 
    PrevTimePart = new DateTime(); 
    TimeSpan difference; 

    try 
    { 
     count++; 
     conn.ConnectionString = "Context Connection=true"; 
     cmd.Connection = conn; 

     String[] string1 = promptdetails.Split(";".ToCharArray()); 
     foreach (var item1 in string1) 
     { 
      count++; 
      String[] string2 = item1.Split(",".ToCharArray()); 
      PromptPart = string2[1]; 
      TimePart = DateTime.ParseExact(string2[0], "M/d/yyyy h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture); 
      if (count > 1) 
      { 
       StringBuilder sbQuery = new StringBuilder(1024); 
       sbQuery.Append("INSERT INTO [Shami].[DBO].[data] (MSISDN,PromptID1,PromptID2,TimeDifference) VALUES"); 
       sbQuery.Append("('"); 
       sbQuery.Append(msisdn); 
       sbQuery.Append("',"); 
       difference = TimePart.Subtract(PrevTimePart); 
       sbQuery.Append("'"); 
       sbQuery.Append(PrevPromptPart); 
       sbQuery.Append("','"); 
       sbQuery.Append(PromptPart); 
       sbQuery.Append("',"); 
       sbQuery.Append(difference.Seconds); 
       sbQuery.Append(")"); 
       string sub = string.Empty; 
       sub = sbQuery.ToString(); 
       try 
       { 
        conn.Open(); 
        cmd = new SqlCommand(sub); 
        SqlContext.Pipe.ExecuteAndSend(cmd); 
       } 
       catch (Exception ie) 
       { 
        Console.WriteLine("Error.."); 
       } 
      } 
      if (count <= 1) 
      { 
       StringBuilder sbQuery = new StringBuilder(1024); 
       sbQuery.Append("INSERT INTO [Shami].[DBO].[data] (MSISDN,PromptID1,PromptID2,TimeDifference) VALUES"); 
       sbQuery.Append("('"); 
       sbQuery.Append(msisdn); 
       sbQuery.Append("',"); 
       sbQuery.Append("'_'"); 
       sbQuery.Append(","); 
       sbQuery.Append(PromptPart); 
       sbQuery.Append(","); 
       sbQuery.Append("'0'"); 
       sbQuery.Append(")"); 
       string sub = string.Empty; 
       sub = sbQuery.ToString(); 
       try 
       { 
        conn.Open(); 
        cmd = new SqlCommand(sub); 
        SqlContext.Pipe.ExecuteAndSend(cmd); 
       } 
       catch (Exception ie) 
       { 
        Console.WriteLine("Error.."); 
       } 
      } 
      PrevPromptPart = PromptPart; 
      PrevTimePart = TimePart; 
     } 



    } 
    catch (Exception) 
    { ;} 
    finally 
    { 
     conn.Close(); 
     conn.Dispose(); 
     cmd.Dispose(); 
    } 
    return msisdn; 

어디에서 잘못 될지 알려 주시기 바랍니다. CLR에서 디버깅 할 수 없습니다.

당신은 당신의 라인

Console.WriteLine ("오류 ...") 교체 할 필요가

+1

한 곳에서 실행됩니다 스트링 빌더를 사용하여 쿼리를 구축하고 (아마 직접 현재의 에러 관련이 없습니다). 당신은 매개 변수 –

+0

'Console.WriteLine ("Error ..");을 사용해서는 안되며, 그럴 가능성이 있습니다. SQL에는 콘솔이 없습니다. – leppie

+0

@leppie : 고맙습니다. 당신 말이 맞습니다. 예외는 없지만 :) 테이블에 삽입 할 수 없습니다. –

답변

0

; 이 라인

:

SqlContext.Pipe.Send ("오류 ...");

이 당신을 위해 같은 일을하고 당신이 잘못된거야