Home  >  Article  >  Backend Development  >  About how C# implements Access to add data queried by time period to ListView

About how C# implements Access to add data queried by time period to ListView

黄舟
黄舟Original
2017-07-18 10:35:031787browse

这篇文章主要介绍了C# 将Access中以时间段条件查询的数据添加到ListView中,需要的朋友可以参考下

一、让ListView控件显示表头的方法

在窗体中添加ListView 空间,其属性中设置:View属性设置为:Detail,Columns集合中添加表头中的文字。

二、利用代码给ListView添加Item。

首先,ListView的Item属性包括Items和SubItems。必须先实例化一个ListIteView对象。具体如下:


ListViewItem  listViewItem=new ListViewItem();
listViewItem.SubItems[0].Text=""11111;//第一行第一例的值
listViewItem.SubItems.Add("222");///
listViewItem.SubItems.Add("222");///以此类推
ListView1.Items.Add(listViewItem);

三、Access中时间段查询的SQL语句书写规范(采用dateTimePick控件)

      注意:#  是必须要加的


string sql=select * from tableName where timeField between #"+dateTimePick1.value.ToString()+"# and #"+dateTimePick2.vlaue.ToString()+"#";

四、连接数据库,按条件查询数据并显示在ListView中


string path = System.Environment.CurrentDirectory + "\\database.mdb";
      OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path);//station2.mdb
      con.Open();
      string sql = "select * from SendRecord where SENDTIME between #" + dateTimePicker1.Value.ToString() + "# and #" + dateTimePicker2.Value.ToString() + "#";
      // string sql = "select * from SendRecord";
      OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
      DataSet dt = new DataSet();
      da.Fill(dt);
      DataTable dtb = dt.Tables[0];
      foreach (DataRow row in dtb.Rows)
      {
        ListViewItem listviewItem = new ListViewItem();
        listviewItem.SubItems.Clear();
        listviewItem.SubItems[0].Text = string.Format("{0:yyyy-MM-dd HH:mm}", row["SENDTIME"]);
        listviewItem.SubItems.Add((string)row["SENDER"]);
        listviewItem.SubItems.Add((string)row["CONTENT"]);
        listviewItem.SubItems.Add(string.Format("{0}", row["AUDITOR"]));
        listviewItem.SubItems.Add(string.Format("{0:yyyy-MM-dd HH:mm}", row["AUDITTIME"]));
        listviewItem.SubItems.Add(string.Format("{0}", row["AUDITSTATUS"]));
        listView1.Items.Add(listviewItem);
      }
      con.Close();
}

The above is the detailed content of About how C# implements Access to add data queried by time period to ListView. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn