繁体   English   中英

C#从SQL查询填充报表

[英]C# Populate Report From SQL Query

我想从DataGridView选择1行,然后将该值作为@CwAgencyKey传递给我的SQL查询where子句。 然后,我将在数据库中查询要填充报表文本框的数据。

string queryString = "SELECT DISTINCT AGENCY_NAME  " +
                        "FROM CW_AGENCY CA WITH(NOLOCK) " +
                        "INNER JOIN CW_KEYS CK WITH(NOLOCK) ON CK.CW_AGENCY_KEY = CA.CW_AGENCY_KEY " +
                        "INNER JOIN CW_MAST CM WITH(NOLOCK) ON CM.CW_KEY = CK.CW_KEY " +
                        "INNER JOIN AGENCY A WITH(NOLOCK) ON A.AGENCY_KEY = CA.PAYEE_KEY " +
                        "WHERE CA.CW_AGENCY_KEY = @CwAgencyKey";

var dataSet = new DataSet("CheckWriter");

try
{
    var dataAdapter = new SqlDataAdapter(queryString, ConnectDB.connectionDB);
    dataAdapter.SelectCommand.Parameters.AddWithValue("@CwAgencyKey", cwAgencyKey);

    form2.reportViewer1.LocalReport.DataSources.Clear();
    DataTable dt = new DataTable();
    dataAdapter.Fill(dt);

    object agencyName = dt.Rows[0][0];

    Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt);

    form2.reportViewer1.LocalReport.ReportPath = "../../Report1.rdlc";

    form2.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);

    ReportParameter payeeReport = new ReportParameter("payeeReport");
    payeeReport.Values.Add(agencyName.ToString());

    form2.reportViewer1.LocalReport.SetParameters(payeeReport);
    form2.reportViewer1.RefreshReport();
}
catch (SqlException ex)
{
    MessageBox.Show("SQL Error: " + ex);
}

此代码在form2.reportViewer1.LocalReport.SetParameters(payeeReport);处产生错误form2.reportViewer1.LocalReport.SetParameters(payeeReport);

Microsoft.Reporting.WinForms.LocalProcessingException:“本地报表处理期间发生错误。”

任何人都可以提供正确的方法来从查询中填充报告吗?

我得到它的工作!

我使用原始发布的查询创建了一个存储过程,除了一个参数(@CwAgencyKey)。 我在Report1.rdlc(报告数据)中创建了一个数据集。 在Form1 DataGridView的双击事件中,我将指定的值传递给DataSet,然后刷新reportViewer1并显示Form2。

private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
    int cwAgencyKey;
    Form2 form2 = new Form2();
    int rowindex = this.dataGridView1.CurrentRow.Index;
    string strCwAgencyKey = "";
    strCwAgencyKey = this.dataGridView1.Rows[rowindex].Cells[0].Value.ToString().Trim();
    cwAgencyKey = Int32.Parse(strCwAgencyKey);
    // TODO: This line of code loads data into the 'databaseDataSet.storedProc' table. You can move, or remove it, as needed.
    form2.storedProcTableAdapter.Fill(form2.databaseDataSet.storedProc, cwAgencyKey);
    form2.reportViewer1.RefreshReport();
    form2.Show();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM