簡體   English   中英

在ASP.NET C#中使用SQLCommand從代碼填充報表查看器

[英]Populate Report Viewer From Code using SQLCommand in ASP.NET C#

我希望能夠在不使用存儲過程的情況下使用 SQLCommand填充報表查看器。 我創建了一個報告rdlc並使用表中的列進行設置。

我在用:

  • Visual Studio 2013
  • .Net Framework 4.5
  • 報表查看器11
  • ASP.NET項目

這是我的代碼:

 var Class_Connection = new SQL_Connection();
                Class_Connection.cnn.Close();
                Class_Connection.cnn.Open();

var cmd = new SqlCommand("select * from TT", Class_Connection.cnn);
var dt = new DataTable();
dt.Load(cmd.ExecuteReader());
var source = new ReportDataSource(dt.TableName, dt);
RV_Main.LocalReport.ReportPath = Server.MapPath("~/CCD/TT/Report/MyReport.rdlc");
RV_Main.LocalReport.DataSources.Add(source);
RV_Main.LocalReport.Refresh();

在這里看到了其他這樣的帖子。 但這是使用存儲過程。 我想在幾行中使用select語句來做到這一點。 有人可以給我舉個例子嗎?

您必須在報表查看器GUI中設置數據源。 然后像這樣在代碼中引用您在數據源中使用的數據集:

var cmd = new SqlCommand("select * from TT", Class_Connection.cnn);
var dt = new DataTable();
dt.Load(cmd.ExecuteReader());
var source = new ReportDataSource("DataSet1", dt);
RV_Main.LocalReport.ReportPath = Server.MapPath("~/CCD/TT/Report/MyReport.rdlc");
RV_Main.LocalReport.DataSources.Add(source);
RV_Main.LocalReport.Refresh();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM