C#查询SQLite并使用DataGrid显示

最近使用C#写上位机,需要显示SQLite数据库中存储的数据,查询了许多资料解决了这个问题,在此记录一下。
界面如下图所示:
![界面](https://img-blog.csdn.net/20160823134450752)
点击查询按钮后根据设置的查询日期,在数据库中查询并显示。
按键的单击事件代码如下:
private void FindButton_Click(object sender, RoutedEventArgs e)
{
     DataTable dataTable = new DataTable();
     string start = StartYear.Text + '-' + StartMoon.Text + '-' + StartDay.Text + ' ' + StartHour.Text + ':' + StartMin.Text + ':' + StartSec.Text;
     string end = EndYear.Text + '-' + EndMoon.Text + '-' + EndDay.Text + ' ' + EndHour.Text + ':' + EndMin.Text + ':' + EndSec.Text;
     cmd.CommandText = string.Format("SELECT * FROM Data where Time between datetime('{0}') and datetime('{1}')", start,end);
     SQLiteDataReader dr = cmd.ExecuteReader();
     dataTable.Load(dr);
     DisplayDataGrid.ItemsSource = dataTable.DefaultView;
}
显示所有列数据则应使用“select * from tablename”。
若只查询显示某一列数据则应把“*”替换为该列的名称。
另外,SQLite的DATETIME格式为“yyyy-MM-dd hh:mm:ss”应注意符合格式,否在会出现错误。
对于DataGrid中时间数据的显示格式可以通过如下代码进行设置:
<DataGridTextColumn Header="时间" Binding="{Binding Path=Time, StringFormat='yyyy-MM-dd HH:mm:ss'}" />
并且将DataGrid的AutoGenerateColumns属性设置为False。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值