将来自内连接的值放入对象中的对象中
我有以下具有内部联接的 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) 中的值放入对象 account 中,该对象本身位于对象 中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;
}
评论有一个构造函数:
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 中的对象 account 中,它会给出 NullReferenceException。 我知道这意味着它没有价值,但我似乎无法找到为什么它没有价值。
我用 sql server 2008 查询设计器检查了我的内部联接,这有效 所以它必须是对象,但我没有看到问题。
请帮助我:)
问候
I have the following sql command with an Inner join:
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();
I want to put the values from a (News_accounts) in the object account that is in itself in the object newsComment which is located in a generic List, List newsComments.
I do that like this:
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;
}
Comments has a constructor:
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;
}
And Accounts account as well:
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;
}
Up until rating all goes well and the values are being put in the newsComment object, however, when it reaches the values that need to be put in the object account that is located in the object newsComment, it gives a NullReferenceException.
I know this means that it doesn't have a value but I can't seem to find why it has no value.
I've looked checked my Inner join with sql server 2008 query designer, and that works
so it's got to be the object but I don't see the problem.
please help me :)
greetings
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
newsComment.account
您必须在访问该对象的字段、属性或方法之前初始化该对象。像这样的事情:...或者您可以从
Comments
类的构造函数中执行此操作,该类不带任何参数。附带说明:也许您应该考虑使用 ORM,例如 LINQ to SQL 或实体框架。这就是他们所做的。
newsComment.account
You have to initialize that object before accessing its fields, properties, or methods. Something like this:... or you can do it from the constructor of the
Comments
class, the one which takes no arguments.As a side note: maybe you should consider using an ORM, like LINQ to SQL or Entity Framework. It's what they do.
您没有实例化您的帐户对象,因此它为空
You are not instantiating your account object so it is null