使用c#windows form datetime picker工具从sqlserver中选择日期

时间:2016-11-29 07:12:07

标签: c# sql-server winforms datetime

亲爱的团队我希望在c#windows窗体应用程序中使用c#datetime picker工具从sqlserver日期时间列获取数据日期,因此下面给出的查询是在不同日期之间检索数据但是当我选择相同的日期查询时不返回数据请帮助解决问题。

   public void TListByDate()
    {
        try
        {
            dataGridView1.Rows.Clear();

            SqlCommand cmd = new SqlCommand("Select Pur_Entry_Date,Pur_Invoice_No,Supplier_Bill_No,Product.Item_Name,Pur_Item_Batch_No,Item_Batch_Expiry,Pur_Item_PackQuantity,Pur_Bonus_LooseQuantity,MRP_Final,Purchase_Pack_Price,Discount_Percentage,Extra_Discount_Percentage,SalesTax_Percent,Pur_Item_Total_Amount from purchase  LEFT OUTER JOIN product on Purchase.Item_Code = Product.Item_Code where Purchase.Pur_Entry_Date   BETWEEN'" + dateTimePicker1.Value.ToString() + "' AND '" + dateTimePicker2.Value.ToString() + "' order by purchase.Pur_Entry_Date", ConnectionDAC.GetConnection());
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();

            adp.Fill(dt);
            if (dt.Rows.Count == 0)
            {
                label8.Text = "Zero Rows found";
            }
            else
            {
                dataGridView1.Rows.Clear();
                int totalrows = dt.Rows.Count;
                label8.Text = totalrows.ToString();
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    DataRow dr = dt.Rows[i];
                    dataGridView1.Rows.Add(dr[0], dr[1], dr[2], dr[3], dr[4], dr[5], dr[6], dr[7], dr[8], dr[9], dr[10], dr[11], dr[12], dr[13]);
                }
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        TListByDate();
    }

1 个答案:

答案 0 :(得分:0)

这是我的意见;

  1. 您应始终使用parameterized queries。这种字符串连接可用于SQL Injection攻击。
  2. 如果您的Purchase.Pur_Entry_Datedatetime,请在CLR端向System.DateTime而不是datetime is mapped with System.DateTime传递string
  3. 您应该使用using statement自动处理SqlCommandSqlDataAdapter
  4. 也是你的

    int totalrows = dt.Rows.Count;
    label8.Text = totalrows.ToString();
    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
         DataRow dr = dt.Rows[i];
         dataGridView1.Rows.Add(dr[0], dr[1], dr[2], dr[3], dr[4], dr[5], dr[6], dr[7], dr[8], dr[9], dr[10], dr[11], dr[12], dr[13]);
    }
    

    对我来说似乎没必要,因为看起来你试图逐个绑定每个单元格值 。而不是那样,只需向你的dt DataSource property提供dataGridView1.DataSource = dt; 就好了;

    var command = @"Select Pur_Entry_Date, Pur_Invoice_No, Supplier_Bill_No, Product.Item_Name, Pur_Item_Batch_No, Item_Batch_Expiry, Pur_Item_PackQuantity, Pur_Bonus_LooseQuantity, MRP_Final, Purchase_Pack_Price, Discount_Percentage, Extra_Discount_Percentage, SalesTax_Percent, Pur_Item_Total_Amount 
                    from purchase  
                    LEFT OUTER JOIN product on Purchase.Item_Code = Product.Item_Code 
                    where Purchase.Pur_Entry_Date BETWEEN @p1 AND @p2 
                    order by purchase.Pur_Entry_Date";
    using(var cmd = new SqlCommand(cmd, ConnectionDAC.GetConnection()))
    {
       cmd.Parameters.Add("@p1", SqlDbType.DateTime).Value = dateTimePicker1.Value;
       cmd.Parameters.Add("@p2", SqlDbType.DateTime).Value = dateTimePicker2.Value;
    
       using(var adp = new SqlDataAdapter(cmd))
       {
           adp.Fill(dt);
       }
    
       if (dt.Rows.Count == 0)
       {
           label8.Text = "Zero Rows found";
       }
       else
       {
          dataGridView1.Rows.Clear();
          label8.Text = totalrows.ToString();
          dataGridView1.DataSource = dt;
       }
    }
    

    最后,我认为你的代码会更好;

    DROP TRIGGER IF EXISTS `update_comment_status`;CREATE DEFINER=`root`@`localhost` TRIGGER `update_comment_status` AFTER UPDATE ON `comments` FOR EACH ROW BEGIN
       DECLARE RESTAURANT_RATE_AVERAGE INTEGER;
       DECLARE RESTAURANT_COMMENT_COUNT INTEGER;
       SET RESTAURANT_RATE_AVERAGE := (SELECT AVG(votes.vote_num) FROM votes);
       UPDATE restaurants res
       INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
       INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
       SET restaurant_rate_average = RESTAURANT_RATE_AVERAGE
       WHERE cm.comment_status = NEW.comment_status;
    
       SET RESTAURANT_COMMENT_COUNT := (SELECT COUNT(votes.vote_id) FROM  votes);
          UPDATE restaurants res
       INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
       INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
       SET restaurant_comment_count = RESTAURANT_COMMENT_COUNT
       WHERE cm.comment_status = NEW.comment_status;
    
    END