다른 클래스 내부에 클래스가있을 때 데이터 리더에서 데이터를 가져 오는 방법은 무엇입니까?

호세 이그나시오 라 데스 카

저는 데이터베이스에서 데이터를 가져와야하는 프로젝트를 진행 중이며 데이터 리더에서 데이터를로드하고 작동하도록 만드는 방법을 작성하는 데 약간의 문제가 있습니다.

이 특정 모델에는 다음과 같이 정의 된 두 가지 클래스가 있습니다.

AUTHOR
private string authFirstName;
private string authLastName;
private Country country;

COUNTRY
private int countryId;
private string countryName;

이것은 내가 작성한 코드 샘플이며 작동하지 않습니다 (비주얼 스튜디오가 잘못된 것으로 표시한다는 의미). 나는 누군가가이 문제에 대해 약간의 도움을 줄 수 있을지 궁금합니다.

protected static Author LoadFromReader(IDataRecord row)
{
    Author a = null;
    if (row != null)
    {
        a = new Author
        {
            AuthFirstName = row.GetString(row.GetOrdinal("authName")),
            AuthLastName = row.GetString(row.GetOrdinal("authLName")),
            country.CountryId = row.GetInt32(row.GetOrdinal("countryID")),

        };
    }
    return a;
}
Nkosi

다음과 같이 정의 된이 두 클래스가 있다고 가정합니다.

public class Author
{
    public string AuthFirstName {get; set;}
    public string AuthLastName {get; set;}
    public Country Country {get; set;}
}

public class Country
{
    public int CountryId {get; set;}
    public int CountryName {get; set;}
}

방법은 다음과 같습니다.

protected static Author LoadFromReader(IDataRecord row)
{
    Author a = null;
    if (row != null)
    {
        a = new Author
        {
            AuthFirstName = row.GetString(row.GetOrdinal("authName")),
            AuthLastName = row.GetString(row.GetOrdinal("authLName")),
            Country = new Country
            {
               CountryId = row.GetInt32(row.GetOrdinal("countryID"))
            },
        };
    }
    return a;
}

Country속성을 할당하기 전에 클래스 의 인스턴스를 만들어야하는 곳

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관