具有2个参数的RDLC报告(存储过程)

时间:2017-10-19 19:25:25

标签: c# winforms rdlc

private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'DataSet1.sp_GetRent1' table. You can move, or remove it, as needed.
        this.sp_GetRent1TableAdapter.Fill(this.DataSet1.sp_GetRent1, "@RentNo1", "@AppPath");
        //this.GetSPResult();
        this.reportViewer1.RefreshReport();
    }

private void button1_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        reportViewer1.Visible = true;
        reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
        reportViewer1.LocalReport.DataSources.Clear();
        reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1",dt));
        GetSPResult();
    }

private DataTable GetSPResult()
    {
        DataTable ResultsTable = new DataTable();
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=RentACar1;Integrated Security=True");// ProviderName = "System.Data.SqlClient");
        try
        {
            SqlCommand cmd = new SqlCommand("sp_GetRent1",conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@RentNo1", "9905-10-2017");
            cmd.Parameters.AddWithValue("@AppPath", @"E:\Rent A Car\RentACar1\RentACar\bin\Debug");
            conn.Open();
            //cmd.ExecuteNonQuery();
            SqlDataAdapter adaptor = new SqlDataAdapter(cmd);
            adaptor.SelectCommand = cmd;
            adaptor.Fill(ResultsTable);
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        return ResultsTable;
    }

此代码未向我显示RDLC报告中的任何内容。如果我打开dataset1.xsd,并向SP提供相同的参数,我会在网格中获取值。但是我无法在RDLC中获得值。

1 个答案:

答案 0 :(得分:0)

你的button1_Click应如下所示:

private void button1_Click(object sender, EventArgs e)
{
    DataTable dt = GetSPResult();
    reportViewer1.Visible = true;
    reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
    reportViewer1.LocalReport.DataSources.Clear();
    reportViewer1.LocalReport.DataSources.Add(new 
    Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dt));
}

请注意,从GetSPResult返回的DataTable应该添加到报表中,而不是像在代码中那样添加到新创建的空白表中。