C# 从对象中的内部联接对象中放入值

C# 从对象中的内部联接对象中放入值,c#,asp.net,inner-join,generic-list,C#,Asp.net,Inner Join,Generic List,我有以下带有内部联接的sql命令: SqlCommand cmd = new SqlCommand(@"SELECT c.comment_Id, c.date, c.comment, c.rating, a.registration_Date, a.username, a.email, a.profile_Image FROM News_comments c INNER JOIN News_accounts a ON c.account_Id=a.account_Id WHERE c.news_

我有以下带有内部联接的sql命令:

SqlCommand cmd = new SqlCommand(@"SELECT c.comment_Id, c.date, c.comment, c.rating, a.registration_Date, a.username, a.email, a.profile_Image FROM News_comments c INNER JOIN News_accounts a ON c.account_Id=a.account_Id WHERE c.news_Id = @news_Id", conn);
cmd.Parameters.Add("news_Id", SqlDbType.Int).Value = news_Id;
conn.Open();
我想把a(News_accounts)中的值放在对象帐户中,对象帐户本身就在对象newsComment中,对象newsComment位于通用列表List newsComments中。 我是这样做的:

using (SqlDataReader reader = cmd.ExecuteReader()) {                
    while (reader.Read()) {
        Comments newsComment = new Comments();
        newsComment.comment_Id = int.Parse(reader["comment_Id"].ToString());
        newsComment.date = DateTime.Parse(reader["date"].ToString());
        newsComment.comment = reader["comment"].ToString();
        newsComment.rating = int.Parse(reader["rating"].ToString());
        newsComment.account.Registration_Date = DateTime.Parse(reader["registration_Date"].ToString());
        newsComment.account.Username = reader["username"].ToString();
        newsComment.account.Email = reader["email"].ToString();
        newsComment.account.Profile_Image = reader["profile_Image"].ToString();
        newsComments.Add(newsComment);
    }
    return newsComments;
}
newsComment.account = new Account();
newsComment.account.Registration_Date = DateTime.Parse(reader["registration_Date"].ToString());
newsComment.account.Username = reader["username"].ToString();
newsComment.account.Email = reader["email"].ToString();
newsComment.account.Profile_Image = reader["profile_Image"].ToString();
注释有一个构造函数:

public Comments(int comment_Id, DateTime date, string comment, int rating, Accounts account) {
    this.comment_Id = comment_Id;
    this.date = date;
    this.comment = comment;
    this.rating = rating;
    this.account = account;
}
以及账户:

public Accounts(int account_Id, DateTime registration_Date, string email, string username, string profile_Image, string homepage, string about, bool admin) {
    this.account_Id = account_Id;
    this.registration_Date = registration_Date;
    this.email = email;
    this.profile_Image = profile_Image;
    this.homepage = homepage;
    this.about = about;
    this.admin = admin;
}
但是,在评级顺利且值被放入newsComment对象之前,当它达到需要放入对象newsComment中的对象帐户的值时,它会给出一个NullReferenceException。 我知道这意味着它没有价值,但我似乎无法找到它没有价值的原因

我已经用SQLServer2008查询设计器检查了我的内部连接,这很有效 所以它一定是物体,但我看不出问题所在

请帮帮我:)


问候语

您没有实例化您的帐户对象,因此它为空

newsComment。帐户
您必须在访问其字段、属性或方法之前初始化该对象。大概是这样的:

using (SqlDataReader reader = cmd.ExecuteReader()) {                
    while (reader.Read()) {
        Comments newsComment = new Comments();
        newsComment.comment_Id = int.Parse(reader["comment_Id"].ToString());
        newsComment.date = DateTime.Parse(reader["date"].ToString());
        newsComment.comment = reader["comment"].ToString();
        newsComment.rating = int.Parse(reader["rating"].ToString());
        newsComment.account.Registration_Date = DateTime.Parse(reader["registration_Date"].ToString());
        newsComment.account.Username = reader["username"].ToString();
        newsComment.account.Email = reader["email"].ToString();
        newsComment.account.Profile_Image = reader["profile_Image"].ToString();
        newsComments.Add(newsComment);
    }
    return newsComments;
}
newsComment.account = new Account();
newsComment.account.Registration_Date = DateTime.Parse(reader["registration_Date"].ToString());
newsComment.account.Username = reader["username"].ToString();
newsComment.account.Email = reader["email"].ToString();
newsComment.account.Profile_Image = reader["profile_Image"].ToString();
。。。也可以从
Comments
类的构造函数中执行,该类不带参数


作为一个旁注:也许你应该考虑使用ORM,比如LINQ to SQL或实体框架。这就是他们所做的。

你的意思是我需要放:newcomment.account=newaccounts();之后:Comments newsComment=新注释()?编辑:是的,就是这样。是的,如果它有一个默认的构造函数,它是一个学校项目,对于这个项目,我们还不允许使用LINQ;)