Calling variables from class (DateTime)

2019-09-06 01:58发布

问题:

I need to call my datetime variable from another class,

this is the code for querying with date condition

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;
        }

My DatePicker 1 and 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

    }

i don't know how to do proper calling of method, im new at this.

回答1:

Your static method accepts two DateTime arguments:

static DataTable GetCustomerRecords(DateTime date, DateTime date2)

While you're only passing one each time:

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

You need to pass both DateTime arguments each time you call your method:

Retrieve.GetCustomerRecords(date, date2)


回答2:

This will be your form (code behind) where you will have three events. Two to select the start date and end date. The last is for your button click event to call your other class.

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;
   }
}