我正在开发一个c#
应用程序,我需要生成一个报告。我使用的是一个数据集,其中填充了来自stored procedure
的数据,该数据从C#代码中获取一个参数。我在report1.rdlc
中创建一个参数,并用文本框中的数据填充它。当我运行应用程序时,我在报表查看器上看不到任何内容。
public void GenerateBranchwiseReport()
{
conn.Open();
SqlCommand BranchReportcmd = new SqlCommand("select [am/bsi name] from masterlookup where [asc type]='BRANCH' group by [am/bsi name]", conn);
SqlDataReader BranchReportread = BranchReportcmd.ExecuteReader();
while (BranchReportread.Read())
{
BranchManagerName.Add(BranchReportread.GetValue(0).ToString());
}
conn.Close();
foreach (string managername in BranchManagerName)
{
conn.Open();
SqlCommand GetReportDatacmd = new SqlCommand();
GetReportDatacmd.CommandText = "USP_BranchwiseReport";
GetReportDatacmd.CommandType = CommandType.StoredProcedure;
GetReportDatacmd.Parameters.Add(new SqlParameter("@BranchManagerName", managername));
GetReportDatacmd.Connection = conn;
GetReportDatacmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(GetReportDatacmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
reportViewer1.Reset();
this.reportViewer1.Visible = true;
string reportname = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
this.reportViewer1.LocalReport.ReportPath = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
ReportParameter[] param = new ReportParameter[1];
param[0] = new ReportParameter("ManagerName", managername);
this.reportViewer1.LocalReport.SetParameters(param);
ReportDataSource ReportBranch = new ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]);
this.reportViewer1.LocalReport.ReportEmbeddedResource = reportname;
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]));
//this.reportViewer1.LocalReport.DataSources.Add(ReportBranch);
this.reportViewer1.LocalReport.Refresh();
//SendEmail();
}
}
答案 0 :(得分:0)
为什么要循环浏览报告中的所有管理器?可能您正在寻找包含所有经理数据的报告。
您正在为每个管理器循环相同的报告???
public void GenerateBranchwiseReport()
{
string ManagerNames="";
conn.Open();
SqlCommand BranchReportcmd = new SqlCommand("select [am/bsi name] from masterlookup where [asc type]='BRANCH' group by [am/bsi name]", conn);
SqlDataReader BranchReportread = BranchReportcmd.ExecuteReader();
while (BranchReportread.Read())
{
if (ManagerNames.length==0)
ManagerNames="'"+BranchReportread.GetValue(0).ToString()+"'";
else
ManagerNames =ManagerNames + ",'" + BranchReportread.GetValue(0).ToString()+ "'";
}
conn.Close();
//So now, You have Multiple Managers in ManagerNames string You can send it to SQL, which must used in Where clause with IN (@Managers) in you Stored Procedure.
conn.Open();
SqlCommand GetReportDatacmd = new SqlCommand();
GetReportDatacmd.CommandText = "USP_BranchwiseReport";
GetReportDatacmd.CommandType = CommandType.StoredProcedure;
GetReportDatacmd.Parameters.Add(new SqlParameter("@BranchManagerName", ManagerNames));
GetReportDatacmd.Connection = conn;
GetReportDatacmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(GetReportDatacmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
reportViewer1.Reset();
this.reportViewer1.Visible = true;
string reportname = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
this.reportViewer1.LocalReport.ReportPath = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
ReportParameter[] param = new ReportParameter[1];
param[0] = new ReportParameter("ManagerName", managername);
this.reportViewer1.LocalReport.SetParameters(param);
ReportDataSource ReportBranch = new ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]);
this.reportViewer1.LocalReport.ReportEmbeddedResource = reportname;
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]));
//this.reportViewer1.LocalReport.DataSources.Add(ReportBranch);
this.reportViewer1.LocalReport.Refresh();
//SendEmail();
答案 1 :(得分:0)
要在C#WinForm项目中使用报表查看器,请将以下代码添加到按钮或form_load中:
string strConnectionString = "Data Source=(local);Initial Catalog=Projects_DB;Integrated Security=True";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("sp_GetProject " + "'0660CAD6-6F1A-4D19-A1FD-17BF3655AC98'");
cmd.CommandType = CommandType.Text;
cmd.Connection = new SqlConnection (strConnectionString);
da.SelectCommand = cmd;
da.Fill(ds,"DataSet1");
reportViewer1.ProcessingMode = ProcessingMode.Local;
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ds.Tables[0]));
reportViewer1.LocalReport.Refresh();
reportViewer1.RefreshReport();
假设您有一个接受@ProjectID参数的存储过程。
如果将这些参数与sql命令一起传递,则必须设置cmd.CommandType = CommandType.Text
。但是,如果您不想传递参数,只需将commandType更改为cmd.CommandType = CommandType.StoredProcedure
。
以下是此示例中使用的存储过程:
CREATE PROC [dbo].[sp_GetProject]
@ProjectID nvarchar(50)=NULL
AS
BEGIN
SELECT ProjectID, ProjectName FROM Projects
WHERE
(@ProjectID IS NULL) OR (ProjectID = @ProjectID)
END