1
我試圖使用下面的代碼,但它不起作用。如何在兩個日期之間創建水晶報表
我使用SQL查詢將報告數據存儲在ReportDataSet
中。稍後,我將此ReportDataSet
設置爲DataSource
爲CrystalReport
對象。
private void btnGenReport_Click(object sender, EventArgs e)
{
CrystalReport1 objRpt = new CrystalReport1();
string sql; //creating sql query
sql = "SELECT invoice.InvoiceNo, invoice.Date, invoiceitems.Name, invoice.InvoiceTo, invoice.CusName, invoiceitems.Qty, invoiceitems.Rate, invoiceitems.Amount ";
sql += "FROM (invoice INNER JOIN invoiceitems ON invoice.Date = invoiceitems.Date) ";
sql += "WHERE (invoice.Date BETWEEN '" + DateTimePickerFrom.Value + "' AND '" + DateTimePickerTo.Value + "');";
ReportDataSet ds = new ReportDataSet();
string path = Application.StartupPath + "\\";
string conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + path + "SMS_DB.mdf;Integrated Security=True;User Instance=True";
SqlConnection con = new SqlConnection(conStr); // create connection
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds, "InvoiceItemData");
objRpt.SetDataSource(ds.Tables["InvoiceItemData"]);
crystalReportViewer1.ReportSource = objRpt;
con.close();
}
catch (Exception ex)
{
MessageBox.Show("Error ! \n" + ex.ToString());
}
}
你的報告沒有你的查詢的WHERE子句嗎?還有,你得到的錯誤究竟是什麼? – 2012-07-26 13:15:09
WHERE子句對我來說看起來不正確。我現在無法檢查,但我相信它應該是'「WHERE(invoice.Date BETWEEN'」+ DateTimePickerFrom.Value.ToShortDateString()+「'AND'」+ DateTimePickerTo.Value.ToShortDateString()+「') 「;' – Ally 2012-07-26 13:24:49
你應該使用sql參數,而不是從字符串構造查詢! http://en.wikipedia.org/wiki/SQL_injection – billy 2012-07-26 13:27:25