从daily_Sale表

时间:2018-04-14 20:06:36

标签: c# sql-server

我正在尝试创建我的零售店的桌面应用程序。

在这个应用程序中,我想计算正在运行的一个月的每日销售,并计算总月,年等的销售

每日促销表的屏幕截图:

enter image description here

我确定这可以计算每日销售情况,并从进入的最后一天开始显示。但是我想在当月展示销售,并在开始时展示下个月的销售。

cmd = new SqlCommand("Select * from Daily_Sale ", con);

adapt = new SqlDataAdapter();
adapt.SelectCommand = cmd;

dt = new DataTable();
adapt.Fill(dt);

bsource = new BindingSource();
bsource.DataSource = dt;
dataGridView1.DataSource = bsource;

chart1.Series["Sale"].XValueMember = "Total_Sale";
chart1.Series["Sale"].YValueMembers = "Total_Sale";
chart1.Series["Profit"].XValueMember = "Total_Profit";
chart1.Series["Profit"].YValueMembers = "Total_sale";
chart1.Series["Margin"].XValueMember = "Margin";
chart1.Series["Margin"].YValueMembers = "Total_sale";

chart1.DataSource = hJSportsDataSet8.Daily_Sale;
chart1.DataBind();

cmd = con.CreateCommand();
cmd.CommandText = "Select * from Daily_Sale ";

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
    chart1.Series["Sale"].XValueMember = "Date_Time";
    chart1.Series["Sale"].YValueMembers = "Total_Sale";
    chart1.Series["Profit"].XValueMember = "Date_Time";
    chart1.Series["Profit"].YValueMembers = "Total_Profit";
    chart1.Series["Margin"].XValueMember = "Date_Time";
    chart1.Series["Margin"].YValueMembers = "Margin";
}

void DailySaleFormcs_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'hJSportsDataSet8.Daily_Sale' table. You can move, or remove it, as needed.
    this.daily_SaleTableAdapter.Fill(this.hJSportsDataSet8.Daily_Sale);

    try
    {
        //MessageBox.Show("today:"+tody);
        cmd = new SqlCommand("SELECT TOP 1 Total_Sale FROM Daily_Sale ORDER BY Date_Time", con);
        cmd.ExecuteNonQuery();

        double tdy_sele = (double)cmd.ExecuteScalar();
        //MessageBox.Show("todays sale" + tdy_sele);
        label_tody.Text = tdy_sele.ToString();

        cmd2 = new SqlCommand("SELECT SUM(Total_Sale) FROM Daily_Sale", con);
        cmd2.ExecuteNonQuery();

        double sum = (double)cmd2.ExecuteScalar();
        // MessageBox.Show("sum :" + sum);
        label_totlsale.Text = sum.ToString();

        cmd = new SqlCommand("SELECT SUM(Total_Profit) FROM Daily_Sale", con);
        cmd.ExecuteNonQuery();
        int proft = (int)cmd.ExecuteScalar();
        label_proft.Text = proft.ToString();

我想尝试以下方法

DateTime dat = DateTime.Today;
MessageBox.Show("today"+dat);
DateTime dayone = new DateTime(dat.AddMonths(1).Year, dat.AddMonths(1).Month, 1);
MessageBox.Show("first day of next month:"+dayone);

但是如何使用while,for循环或条件或其他?

请帮帮我

1 个答案:

答案 0 :(得分:0)

我不知道您的数据库中的数据表是如何准确的,但只使用以下查询(和代码)来获取给定月份的数据(在您的情况下,当前月份):

  int month = Convert.ToInt32(DateTime.Now.Month.ToString());
  int year = 2018;

  cmd = new SqlCommand("Select * from Daily_Sale WHERE MONTH(the column name that has dates) = " + month + " AND YEAR(the column name that has dates) =" + year + "", con);

<强>更新

在评论中进行了一些讨论之后,我清楚地了解了OP的情况,我认为这正是您所寻找的:

  ////A class to handle the data of the database

  public class mydata()
  {
    public string name;
    public string dates;
    ///create required variables of required type for each column :)
  }

现在我们可以使用IDataReader来帮助我们获取所需的数据,并使用List<mydata>来保存过滤后的数据:

  List<mydata> filteredData = new List<mydata>();
  ///As OP commented,the date/time look like :  Saturday, April 14, 2018
  string m0nth = "August" 
  //Or      
  string month = DateTime.Now.Month.ToString()
  cmd = new SqlCommand("Select * from Daily_Sale ", con);
  SqlDataReader dr = cmd.ExecuteReader()
  While (dr.Reader())
   {
    string mnth = dr[2].ToString().Split(', ',', ')[0];  ///change 2 with the column count of the date column
    string finalMonth = mnth.Split(" ")[0];

    if (finalMonth == month)
    {
     mydata dt = new mydata();
     dt.name = dr[0].ToString();
     //keep continuing for each column/each variable in the class we created

    filteredData.Add(dt);
  }}}

最后,我们在datagridview中显示列表:

   foreach(mydata dt in filteredData)
    {
    DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
    row.Cells[0].Value = dt.name;
    ///Keep continuing for each column
    yourDataGridView.Rows.Add(row);
     }