从数据库读取到列表视图 [英] Reading from database to listview

查看:58
本文介绍了从数据库读取到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,将数据库中的数据读入列表视图时遇到了一些问题,我下面的代码无法正常工作,我也不知道为什么会有任何想法!
更改日期时不会出现编译错误.

Hi all, having bit of a problem with reading data from a db into a listview the code I have below doesn''t work and I have no idea why any ideas would be appreciate!
I don''t get an error on compile just when I change the date.

using(connString)
{
conn.Open();
SqlCommand command = new SqlCommand("SELECT FoodName,Calories,Quantity,fat,Protein,Carbs FROM [User_Food] WHERE Date = @Date",conn);
command.Parameters.Add(new SqlParameter("@Date, datePicker1.SelectedDate));
command.ExecuteNonQuery();

SqlDataReader dr = command.ExecuteReader();

while (dr.Read())
{
FoodCollection.Add(new FoodData
{
FoodName = dr.GetString(0),
Calories = dr.GetInt16(1),
Quantity = dr.GetInt16(2),
FatContent = dr.GetDouble(3),
ProteinContent = dr.GetDouble(4),
CarbsContent = dr.GetDouble(5)
});

}
}







Any help would be appreciated very much!

推荐答案

,最初您没有selectedDate.加载页面时,将SelectedDate设置为DateTime.Now;
initially you do not have a selectedDate. When loading the page set the SelectedDate to DateTime.Now;


这篇关于从数据库读取到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆