1

테이블에서 데이터를 읽고 루프하는 CLR UDF를 작성해야하지만 가장 중요한 데이터는 double 배열 인 (이 테이블에는 double 값만 있음)에 저장하는 것이 중요합니다. 내가 검색 한하지만 데이터베이스에 연결하는 예를 발견CLR UDF에서 테이블을 반복 함 C#

, 나는 C# code으로 .DLL을 만들고 싶어하고, 는 저장된 프로 시저에서 호출 ... 몇 가지를 계산하는 수학 라이브러리를 사용합니다.

예를 들어 나는 이것을 발견했다. 그러나 db에 연결하는 대신 dll을 만드는 단계는 어떻게되며 배열에 double 값을 저장합니까?

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Data.SqlTypes; 
using Microsoft.SqlServer.Server; 
using System.Text; 


public partial class StoredProcedures 
{ 
    [Microsoft.SqlServer.Server.SqlProcedure] 
    public static void CLR_StoredProcedure3() 
    { 
     SqlConnection conn = new SqlConnection(); 
     conn.ConnectionString = "Context Connection=true"; 

     SqlCommand cmd = new SqlCommand(); 
     cmd.Connection = conn; 

    } 
} 
+0

이 질문은 당신이 오늘 좀 일찍 게시 한 사람을 대체합니까? – stakx

+0

일종의,하지만이 하나는 내가 thiught 다른 솔루션입니다 ... – cMinor

답변

2

내가 생각하는 가장 효율적인 방법은 두 단계로 그것을 할 수 있습니다 :

int count; 
using (SqlCommand cmdCount = conn.CreateCommand()) 
{ 
    cmdCount.CommandText = "SELECT COUNT(*) FROM [MyTable]"; 
    count = (int)cmdCount.ExecuteScalar(); 
} 

// knowing the number of rows we can efficiently allocate the array 
double[] values = new double[count]; 

using (SqlCommand cmdLoad = conn.CreateCommand()) 
{ 
    cmdLoad.CommandText = "SELECT * FROM [MyTable]"; 

    using(SqlDataReader reader = cmdLoad.ExecuteReader()) 
    { 
     int col = reader.GetOrdinal("MyColumnName"); 
     for(int i = 0; i < count && reader.Read(); i++) 
     { 
      values[i] = reader.GetDouble(col); 
     } 
    } 
} 

// do more processing on values[] here 
+0

멋진 솔루션, 오직 하나의 질문은 거기에 'conn.CreateCommand'를 사용하여 연결을 피할 수있는 방법이 있나요 아니면 그렇게한다면, 무엇 그것에 코드가 있습니까? 'SqlConnection conn = 새로운 SqlConnection(); conn.ConnectionString = "컨텍스트 연결 = true"; SqlCommand cmd = 새 SqlCommand(); cmd.Connection = conn;'충분하니? – cMinor

+0

네, 단지'SqlCommand'를 인스턴스화하고'Connection' 속성도 설정할 수 있습니다. 난 단지 1 라인 및 잠재적으로 * IDBConnection의 일부 다른 구현을 할 수 있습니다 명령 개체 (어떤 경우에는 SQL CLR 코드의 컨텍스트에서 그렇지 않은 경우) 팩토리 메서드 (CreateCommand)를 사용하여 선호 안전한). – Serguei