Calling variables from class (DateTime)

可紊 提交于 2020-01-17 04:40:09

问题


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


来源:https://stackoverflow.com/questions/29850817/calling-variables-from-class-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!