2014-04-30 6 views
0

나는 AA 테이블을 TBL_Person 오와 열이 있습니다테이블의 특정 열을 데이터 집합에 추가하려면 어떻게합니까?

Person_ID, FirstName, LastName, Age, Location 

을 나는 데이터 집합 반환하는 방법이 있습니다

Person_ID, FirstName, LastName, Age, Location 
:이 방법은 열이있는 데이터 집합을 반환합니다

public DataSet GetPerson() 
{ 
    SqlCommand _select = new SqlCommand(); 
    _select.CommandText = "SP-GetAllPerson"; 
    _select.CommandType = System.Data.CommandType.StoredProcedure; 
    _select.Connection = Connection.GetConnection; 

    SqlDataAdapter _daPerson = new SqlDataAdapter(_select); 

    DataSet _personDs = new DataSet(); 
    _daCountry.Fill(_personDs, "[TBL_Person]"); 

    return _personDs; 
} 

하지만 내 메서드에서 다음 열을 사용하여 데이터 집합을 반환하고 싶습니다.

FirstName, LastName, Age 

어떻게하면됩니까?

+0

또한 귀하의 sp 코드를 표시하십시오. –

+1

무슨 뜻인지 _ "데이터 세트에 특정 표의 열을 어떻게 추가합니까"_? 그에 따라 저장 프로 시저를 변경하십시오. 만약 당신이 C#에서 그것을 변경하고 싶다면 질문은 "어떻게 ** ** 데이터 세트의 테이블에서 특정 컬럼을 제거 할 수 있습니까?"? –

+0

데이터 세트에 표시 할 열을 선택하십시오. –

답변

1

모든 열을 선택하지 않으려는 경우 저장된 프로 시저를 적절하게 변경하십시오.

당신은 당신이 table.Columns.Remove(name)를 통해 불필요한 열을 제거 할 수 있습니다 C#으로 변경하려면 다음 원하는 돈`t 변경할 경우

DataSet dsPerson = GetPerson(new[]{"FirstName", "LastName", "Age"}); 
1

:

public DataSet GetPerson(IEnumerable<string> wantedColumns) 
{ 
    using(SqlConnection connection = new SqlConnection("Connection-String")) 
    using (SqlDataAdapter _daPerson = new SqlDataAdapter("SP-GetAllPerson", connection)) 
    { 
     _daPerson.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure; 
     DataSet _personDs = new DataSet(); 
     _daPerson.Fill(_personDs, "TBL_Person"); 
     DataTable tblPersonIds = _personDs.Tables["TBL_Person"]; 
     var allColumns = tblPersonIds.Columns.Cast<DataColumn>().Select(c => c.ColumnName); 
     // remove unwanted columns: 
     foreach (string columnToRemove in allColumns.Except(wantedColumns)) 
      tblPersonIds.Columns.Remove(columnToRemove); 
     return _personDs; 
    } 
} 

당신은이 방법으로이 메소드를 호출 데이터베이스에있는 SP가 다음과 같이 .cs 파일에서 GetPerson() 메소드를 변경합니다.

public DataSet GetPerson() 
{ 
    SqlCommand _select = new SqlCommand(); 
    _select.CommandText = "SP-GetAllPerson"; 
    _select.CommandType = System.Data.CommandType.StoredProcedure; 
    _select.Connection = Connection.GetConnection; 
    SqlDataAdapter _daPerson = new SqlDataAdapter(_select); 
    DataSet _personDs = new DataSet(); 
    _daPerson.Fill(_personDs, "[TBL_Person]"); 
    _personDs.Tables["TBL_Person"].Columns.Remove("Person_ID"); 
    _personDs.Tables["TBL_Person"].Columns.Remove("Location"); 
    return _personDs; 

} 

그렇지 않으면 저장 프로 시저를 적절하게 변경할 수 있습니다.

관련 문제