如何将日期作为参数传递给crystal reportviewer [英] How do I pass date as my parameter to crystal reportviewer

查看:74
本文介绍了如何将日期作为参数传递给crystal reportviewer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表一个表包含产品信息,另一个表包含日期信息。我想从日期选择器的选定日期使用这两个表生成水晶报告。我可以生成没有日期范围的报告。

所以如何将这两个日期参数传递给我的crystalreportviewer。



什么我试过了:



private ProductSell Getdata()

{

DateTime sdt = dateTimePicker1 .Value.Date;

DateTime edt = dateTimePicker2.Value.Date;

SqlConnection connection = new SqlConnection(cn);



SqlCommand cmd = new SqlCommand(选择ProductName,sum(ProductCost)为ProductCost,Sum(ProductPrice)为ProductPrice,sum(ProductQuantity)为ProductQuantity,来自ProductSell,其中GROUP BY ProductName,连接);



// SqlCommand cmd = new SqlCommand(SELECT ProductSell.ProductName,sum(ProductSell.ProductCost)as pc,sum(ProductSell.ProductPrice)as pp,sum(ProductSell.ProductQuantity) as pq FROM ProductSell INNER JOIN Project ON ProductSell.ProjectID = Project.ProjectID WHERE( Project.Date> ='+ sdt +')AND(Project.Date< ='+ edt +')GROUP BY ProductSell.ProductName,connection);



SqlDataAdapter sda = new SqlDataAdapter(cmd);

ProductSell dsproductsell = new ProductSell();

sda.Fill(dsproductsell,ProdSellDT);

返回dsproductsell;

}



private void button1_Click(object sender,EventArgs e)

{

ProductSellReport psreport = new ProductSellReport();

ProductSell dsproductsell = Getdata();

psreport.SetDataSource(dsproductsell);

this.ProductSellReportViewer.ReportSource = psreport;

this.ProductSellReportViewer.RefreshReport();

}

I have two table one table contain product information and another table contain date information.I want to generate crystal report using these two tables from selected date from date picker. i am able to generate report without date range.
so how to pass those two date parameter to my crystalreportviewer.

What I have tried:

private ProductSell Getdata()
{
DateTime sdt = dateTimePicker1.Value.Date;
DateTime edt = dateTimePicker2.Value.Date;
SqlConnection connection= new SqlConnection(cn);

SqlCommand cmd = new SqlCommand("Select ProductName,sum(ProductCost)as ProductCost,Sum(ProductPrice)as ProductPrice,sum(ProductQuantity)as ProductQuantity from ProductSell Where GROUP BY ProductName", connection);

// SqlCommand cmd = new SqlCommand("SELECT ProductSell.ProductName, sum(ProductSell.ProductCost)as pc,sum(ProductSell.ProductPrice)as pp,sum(ProductSell.ProductQuantity)as pq FROM ProductSell INNER JOIN Project ON ProductSell.ProjectID = Project.ProjectID WHERE (Project.Date >='"+sdt+"') AND (Project.Date <='"+edt+"') GROUP BY ProductSell.ProductName", connection);

SqlDataAdapter sda = new SqlDataAdapter(cmd);
ProductSell dsproductsell = new ProductSell();
sda.Fill(dsproductsell, "ProdSellDT");
return dsproductsell;
}

private void button1_Click(object sender, EventArgs e)
{
ProductSellReport psreport = new ProductSellReport();
ProductSell dsproductsell = Getdata();
psreport.SetDataSource(dsproductsell);
this.ProductSellReportViewer.ReportSource = psreport;
this.ProductSellReportViewer.RefreshReport();
}

推荐答案

试试这个

try like this
SqlCommand cmd = new SqlCommand("SELECT ProductSell.ProductName, sum(ProductSell.ProductCost)as pc,sum(ProductSell.ProductPrice)as pp,sum(ProductSell.ProductQuantity)as pq FROM ProductSell INNER JOIN Project ON ProductSell.ProjectID = Project.ProjectID WHERE (Project.Date >=@sdt) AND (Project.Date <=@edt) GROUP BY ProductSell.ProductName", connection);
              cmd.Parameters.Add("@sdt", startDateObj);
              cmd.Parameters.Add("@sdt", endDateObj);





总是使用参数化查询以防止SQL Server中的SQL注入攻击 [ ^ ]


这篇关于如何将日期作为参数传递给crystal reportviewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆