从类调用变量(DateTime)

时间:2015-04-24 14:58:09

标签: c# class datetime methods

我需要从另一个类

调用我的datetime变量

这是查询日期条件的代码

static DataTable GetCustomerRecords(DateTime date, DateTime date2)
        {
            var connSettings = ConfigurationManager.ConnectionStrings["MyDB"];
            {

                string CN = connSettings.ConnectionString;
                MySqlConnection conn = new MySqlConnection(CN);
                MySqlCommand cmd = new MySqlCommand("select value1, value2, value3, date2 from dummy table where date1 >= CAST(@startDate AS DATE) and date2 <= CAST(@endDate AS DATE)", conn);
                cmd.Parameters.AddWithValue("@startDate", date);
                cmd.Parameters.AddWithValue("@endDate", date2);
                MySqlDataAdapter data = new MySqlDataAdapter(cmd);
                conn.Open();
                DataTable dt = new DataTable();
                data.Fill(dt);
                return dt;
            }

        }

 public object QueryRecords(DateTime date, DateTime date2)
        {
            DataTable table = GetCustomerRecords();
            return table;
        }

我的DatePicker 1和2 ValueChanged

 GetRecords Retrieve = new GetRecords();
 private void datePicker1_ValueChanged(object sender, EventArgs e)
    {
        Retrieve.GetCustomerRecords(date)/error here of course
    }

    private void datePicker2_ValueChanged(object sender, EventArgs e)
    {
        Retrieve.GetCustomerRecords(date2)/error here of course

    }

我不知道如何正确地调用方法,我是新手。

2 个答案:

答案 0 :(得分:0)

您的static方法接受两个DateTime个参数:

static DataTable GetCustomerRecords(DateTime date, DateTime date2)

虽然你每次只传递一次:

Retrieve.GetCustomerRecords(date)
Retrieve.GetCustomerRecords(date2)

每次调用方法时都需要传递两个DateTime个参数:

Retrieve.GetCustomerRecords(date, date2)

答案 1 :(得分:0)

这将是您的表单(代码隐藏),您将有三个事件。两个选择开始日期和结束日期。最后一个是您的按钮点击事件来调用您的其他课程。

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
  this.StartDate = dateTimePicker1.Value.Date;
}

private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
  this.EndDate = dateTimePicker1.Value.Date;
}

private void button2_Click(object sender, EventArgs e)
{
  DataTable results = GetCustomerRecords.GetCustomerRecord(this.StartDate, this.EndDate);
}




public static class GetCustomerRecords
{
  public static DataTable GetCustomerRecord(DateTime date, DateTime date2)
  {
    DataTable dt = new DataTable();
    string connStr = ConfigurationManager.ConnectionStrings["YourConnStr"].ConnectionString;
    string query = @"select 
                      value1, 
                      value2, 
                      value3, 
                      date2 
                     from TABLE 
                     where date1 BETWEEN 
                     CAST(@startDate AS DATE) and CAST(@endDate AS DATE)";

    using(SqlConnection conn = new SqlConnection(connStr))
    using (SqlCommand cmd = new SqlCommand(query, conn))
    {
      cmd.Parameters.AddWithValue("@startDate", date);
      cmd.Parameters.AddWithValue("@endDate", date2);
      SqlDataAdapter adapter = new SqlDataAdapter();
      adapter.Fill(dt);
    }

     return dt;
   }
}