2017-12-11 3 views
0

나는 SQL Server 데이터베이스에 대해 SQL SELECT를 기반으로로드 DataTable에 대해 다음 보조 기능 (간체 조금)이 있습니다DataTable을 채우는 SELECT에서 SQL Server의 원래 스키마를 얻는 방법?

static private DataTable GetData_(string connectionString, string sqlSelect, bool doFillSchema = false) 
{ 
    using (SqlConnection connection = new SqlConnection(connectionString)) 
    using (SqlDataAdapter adapter = new SqlDataAdapter(sqlSelect, connection)) 
    { 
     DataTable table = new DataTable(); 

     if (doFillSchema) 
      adapter.FillSchema(table, SchemaType.Mapped); 

     adapter.Fill(table); 
     return table; 
    } 
} 

나중의 목표는 내용을 내 보내야 다른 함수에 반환 table을 통과하는 것입니다 DBF 테이블 (CREATE TABLE)에 넣고 엔진의 다른 기능과 관련된 일부 수정 사항을 사용하여 내용을 작성하십시오.

구현시 원래 numeric(10, 2) 열 유형이 스키마의 Decimal으로 변경됩니다. 원본을 보존하고 싶습니다.

잘 이해한다면 원래의 스키마로 DataTable schemaTable을 가져와야합니다 (어댑터에서 열 유형을 변경하기 전에). 내 생각은 그 같은 기능을 수정하는 것이었다 :

static private DataTable GetData_(string connectionString, string sqlSelect, 
            bool doFillSchema, ref DataTable schemaTable) 
{ 
    using (SqlConnection connection = new SqlConnection(connectionString)) 
    using (SqlDataAdapter adapter = new SqlDataAdapter(sqlSelect, connection)) 
    { 
     DataTable table = new DataTable(); 

     if (doFillSchema) 
      ??? fill the schema table 

     adapter.Fill(table); 
     return table; 
    } 
} 

기능을 어떻게 구현해야합니까? 목적을 위해 SqlDataAdapter을 사용해야합니까? 아니면 다르게해야합니까?

답변

0

운임, 나는이 자기 대답이있다. 더 나은 해결책이 있다면 기꺼이 받아 들일 것입니다. ;)

static private DataTable GetData_(string connectionString, string sqlSelect, Hashtable arg, 
            bool doFillSchema, ref DataTable schemaTable) 
{ 
    using (SqlConnection connection = new SqlConnection(connectionString)) 
    using (SqlCommand command = new SqlCommand(sqlSelect, connection)) 
    { 
     connection.Open(); 

     // If we want the schema, do the extra step. 
     if (doFillSchema) 
     { 
      using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.KeyInfo)) 
       schemaTable = reader.GetSchemaTable(); 
     } 

     // Use the adapter to fill the result table.. 
     using (SqlDataAdapter adapter = new SqlDataAdapter(command)) 
     { 
      DataTable table = new DataTable(); 

      // Set the SELECT arguments. 
      if (arg != null) 
      { 
       foreach (DictionaryEntry item in arg) 
       { 
        adapter.SelectCommand.Parameters.AddWithValue(item.Key.ToString(), item.Value); 
       } 
      } 

      // Get the result of the SQL query. 
      adapter.Fill(table); 
      return table; 
     } 
    } 
} 
관련 문제