Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当一个类位于另一个类中时,如何从datareader中获取数据?_C#_Asp.net_.net_Visual Studio - Fatal编程技术网

C# 当一个类位于另一个类中时,如何从datareader中获取数据?

C# 当一个类位于另一个类中时,如何从datareader中获取数据?,c#,asp.net,.net,visual-studio,C#,Asp.net,.net,Visual Studio,我正在做一个项目,它需要从数据库中获取数据,我在编写从datareader加载数据并使其工作的方法时遇到了一些问题 在这个特定的模型中,除其他外,我有以下两个类,定义如下: AUTHOR private string authFirstName; private string authLastName; private Country country; COUNTRY private int countryId; private string countryName; public clas

我正在做一个项目,它需要从数据库中获取数据,我在编写从datareader加载数据并使其工作的方法时遇到了一些问题

在这个特定的模型中,除其他外,我有以下两个类,定义如下:

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

COUNTRY
private int countryId;
private string countryName;
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;}
}
这是我编写的代码示例,它不起作用,这意味着VisualStudio将其标记为错误。我不知道有谁能在这件事上帮我一点忙

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;
}

假设您有这两个类,定义如下:

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

COUNTRY
private int countryId;
private string countryName;
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类的实例

visual studio给您的错误是什么?如果您可以包含有关您要求我们修复的错误的信息,将对我们有很大帮助。什么是Country?我在代码中没有看到任何东西显示最初声明的位置。