C# 使用“获取两个日期之间的数据”;介于;C中的运算符#
您好,我正在尝试从数据库获取一些数据,并将它们附加到C#中的listview中,我尝试检索的数据介于用户指定的startdate和enddate之间。我将尽可能具体:C# 使用“获取两个日期之间的数据”;介于;C中的运算符#,c#,visual-studio-2010,ms-access,visual-c#-express-2010,C#,Visual Studio 2010,Ms Access,Visual C# Express 2010,您好,我正在尝试从数据库获取一些数据,并将它们附加到C#中的listview中,我尝试检索的数据介于用户指定的startdate和enddate之间。我将尽可能具体: 我正在使用access.mdb数据库 我正在用VisualC#2010开发winforms 我有一个由一个combobox和一个listView组成的表单,combobox有12个项,一年中每个月一个,我试图实现的是将access数据库中的数据附加到该月的listView中 有关守则如下: 这是ComboBox的item se
- 我正在使用access.mdb数据库
- 我正在用VisualC#2010开发winforms
private void cbMonth_SelectedIndexChanged(object sender, EventArgs e)
{
//CONEXION is a constant string with the path to the database
connectDB = new OleDbConnection(CONEXION);
//getting the selected month from the combo
int month = cbMonth.SelectedIndex;
//instanciating a string for the query
string query = getQuery(month);
// debugging XD
MessageBox.Show(query);
// this will be the dataTable with the data retrieved
DataTable EventTable = connectDB(query);
//filling the listview
fillListView(EventTable);
conexionDB.Close();
}
现在这个方法就是返回查询字符串的方法,它非常简单,但我认为问题出在这里,所以请注意xd
public string getQuery(int month)
{
string query = "";
//just placeholders
DateTime ini = DateTime.Now, fin = DateTime.Now;
switch (month)
{
case 0:
ini = DateTime.Parse("01/01/2013");
fin = DateTime.Parse("31/01/2013");
break;
case 1:
ini = DateTime.Parse("01/02/2013");
fin = DateTime.Parse("28/02/2013");
break;
case 2:
ini = DateTime.Parse("01/03/2013");
fin = DateTime.Parse("31/03/2013");
break;
case 3:
ini = DateTime.Parse("01/04/2013");
fin = DateTime.Parse("30/04/2013");
break;
case 4:
ini = DateTime.Parse("01/05/2013");
fin = DateTime.Parse("31/05/2013");
break;
case 5:
ini = DateTime.Parse("01/06/2013");
fin = DateTime.Parse("30/06/2013");
break;
case 6:
ini = DateTime.Parse("01/07/2013");
fin = DateTime.Parse("31/07/2013");
break;
case 7:
ini = DateTime.Parse("01/08/2013");
fin = DateTime.Parse("31/08/2013");
break;
case 8:
ini = DateTime.Parse("01/09/2013");
fin = DateTime.Parse("30/09/2013");
break;
case 9:
ini = DateTime.Parse("01/10/2013");
fin = DateTime.Parse("31/10/2013");
break;
case 10:
ini = DateTime.Parse("01/11/2013");
fin = DateTime.Parse("30/11/2013");
break;
case 11:
ini = DateTime.Parse("01/12/2013");
fin = DateTime.Parse("31/12/2013");
break;
}
query = "select * from Events where (EventsDate between #" +ini.ToShortDateString() + "# and #" + fin.ToShortDateString() + "#)";
return query;
}
这个方法返回我测试过的预期查询
现在是进行查询的方法,我认为这个方法也相当简单
public DataTable connectDB(string query)
{
conexionDB = new OleDbConnection(CONEXION);
//making the query
OleDbDataAdapter adapter = new OleDbDataAdapter(query, conexionDB);
// filling a datatable
DataTable EventTable= new DataTable();
adapter.Fill(EventTable);
return EventTable;
}
现在,这是填充listview的方法
public void fillListView(DataTable EventsTable)
{
for (int i = 0; i < EventsTable.Rows.Count; i++)
{
DataRow row = EventosTable.Rows[i];
string nae= row["EventName"].ToString();
MessageBox.Show(name);
DateTime date = DateTime.Parse(fila["EventPrice"].ToString());
int price = int.Parse(fila["EvenPrice"].ToString());
string description = fila["EventDescrip"].ToString();
Event myevent = new Event();
myevent.Name = name;
myevent.Date= date;
myevent.Price= price;
myevent.Description= description;
EventsListView.Add(myevent);
ListViewItem item = new ListViewItem();
item.Text = name;
item.SubItems.Add(date.ToString());
item.SubItems.Add(price.ToString());
item.SubItems.Add(description.ToString());
EventsListView.Items.Add(item);
}
}
public void fillListView(DataTable EventsTable)
{
for(int i=0;i
嗯,一切都很好。。问题是,如果我在组合框中选择比任何事件大一个月,listview将每月加载一次,即尝试以下两种方法之一: 1) 将日期字符串更改为MM/dd/yyyy格式:
case 10:
ini = DateTime.Parse("12/01/2013");
fin = DateTime.Parse("12/31/2013");
break;
2) 或者使用DateTime.ParseExact()方法指定格式:
case 11:
ini = DateTime.ParseExact("01/12/2013", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
fin = DateTime.ParseExact("31/12/2013", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
break;