如何在Crystal C#中传递值 [英] How to pass value in crystal report C#
问题描述
你好
在我的INVOICE应用程序中,有2个表单首先将产品添加到我的发票数据库中,带有账单ID,第二个带有水晶报告
在我的第一个表单中,我有一个打印按钮,当我单击该打印按钮时,我们将报告表格,我将需要来自form1的帐单ID,并在我的水晶报表中显示该帐单ID的所有产品/>
PLZ帮帮我
我尝试过的事情:
SqlConnection cnn;
string connectionString = null ;
string sql = null ;
connectionString = 数据源= SQLEXPRESS;初始目录= MyBill; Integrated Security = True;池=假跨度>;
cnn = new SqlConnection(connectionString);
cnn.Open();
sql = SELECT * FROM sale其中billid =' + nn + ';
SqlDataAdapter dscmd = new SqlDataAdapter(sql,cnn);
DataSet ds = new DataSet();
dscmd.Fill(ds);
// MessageBox.Show(ds.Tables [1] .Rows.Count.ToString());
cnn.Close();
CrystalReport2 objRpt = new CrystalReport2();
objRpt.SetDataSource(ds);
crystalReportViewer1.ReportSource = objRpt;
crystalReportViewer1.Refresh();
没有指出可用的示例和教程的数量..
我通常创建一个针对数据库的报告我不使用数据集。我还通常使用报告存储过程 - 当您有子报告时,它们更快并且具有明显的优势。以下链接将向您展示如何使用数据集;
代码项目 - 使用带数据集的C#创建Crystal Reports [ ^ ]
您需要添加以下引用 - 假设Windows窗体;
CrystalDecisions.CrystalReports.Engine
CrystalDecisions.ReportSource
CrystalDecisions.Shared
CrystalDecisions.Windows.Forms
我使用VS 2015与Crystal Reports 13.2 - 请参阅下面的链接以获取运行时下载;
Crystal Reports,Visual Studio开发人员下载 - 商业智能(BusinessObjects) - SCN Wiki [ ^ ]
您还需要添加以下使用
使用 CrystalDecisions.CrystalReports.Engine;
使用 CrystalDecisions.Shared;
你还没有指定哪个报告 - 这是按如下方式完成的;
ReportDocument rptDoc = new ReportDocument();
rptDoc.Load( 将完整路径和文件名放在这里);
然后循环报告参数并设置它们 - 我通常使用以下内容;
if (rptDoc.DataDefinition.ParameterFields.Count > 0 )
{
foreach (ParameterFieldDefinition crDef in rptDoc.DataDefinition.ParameterFields)
{
// 检查空报告名称
// 子报告将有一个值,主报告不
// 子报告参数由主报告传递
如果(crDef.ReportName == string .Empty)
{
object objValue = 我想用什么价值;
rptDoc.SetParameterValue(crDef.ParameterFieldName,objValue);
}
}
}
接下来要做的是创建一个Crystal报表ConnectionInfo,然后将其传递给Report
ConnectionInfo crConn = new ConnectionInfo();
crConn.ServerName = 我的数据库服务器名称;
crConn.DatabaseName = 我的数据库名称;
crConn.UserID = 数据库用户名;
crConn.Password = DB Password;
// 获取报告表
表crTables = rptDoc.Database.Tables ;
for ( int i = 0 ; i < crTables.Count; i ++)
{
Table crTable = crTables [i];
TableLogOnInfo tblInfo = crTable.LogOnInfo;
tblInfo.ConnectionInfo = crConn;
crTable.ApplyLogOnInfo(tblInfo);
}
// 然后显示报告 - 这里我使用的是Crystal Reports Viewer Windows窗体
此 .crystalReportsViewerFrm.ReportSource = rptDoc;
好运
这是运行示例,我与您分享:
数据库方法:
public DataSet SelectAll_()
{
var ds = new DataSet();
var conn = DACUtil.GetConnection();
try
{
SqlCommand cmd = new SqlCommand(SELECT_ALL,conn);
var da = new SqlDataAdapter {SelectCommand = cmd};
da.Fill(ds);
ds.Tables [0] .TableName =Employee;
ds.AcceptChanges();
}
最后是
{
conn?.Dispose();
}
返回ds;
}
在ReportViewer.cs文件中:
public partial class ReportViewer:System.Web.UI.Page
{
ReportDocument _rpt = new ReportDocument();
EmployeeDAC employee = new EmployeeDAC();
protected void Page_Load(object sender,EventArgs e)
{
_rpt.Load(Server.MapPath(EmployeeReport.rpt));
DataSet ds = employee.SelectAll_();
_rpt.SetDataSource(ds);
CrystalReportViewer1.ReportSource = _rpt;
}
}
有关详细信息,请参阅以下文章。
使用C# Asp.net 2015中的Crystal报告 [ ^ 一>
hello
In my INVOICE app there are 2 forms first to add products to my invoice database with bill ID and second with crystal report
In my first form i have one print button which take us to report form what i want is when i click that print button it will take that bill id from form1 and display all the product of that bill id in my crystal report
plz help me
What I have tried:
SqlConnection cnn;
string connectionString = null;
string sql = null;
connectionString = "Data Source=SQLEXPRESS;Initial Catalog=MyBill;Integrated Security=True;Pooling=False";
cnn = new SqlConnection(connectionString);
cnn.Open();
sql = "SELECT * FROM sale where billid = '"+nn+"'";
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
DataSet ds = new DataSet();
dscmd.Fill(ds);
// MessageBox.Show(ds.Tables[1].Rows.Count.ToString());
cnn.Close();
CrystalReport2 objRpt = new CrystalReport2();
objRpt.SetDataSource(ds);
crystalReportViewer1.ReportSource = objRpt;
crystalReportViewer1.Refresh();
Without pointing out the number of examples and tutorials that are available..
I typically create a Report against the database I don't use datasets. I also typically use Stored Procedures for Reports - they are quicker and have distinct advantages when you have sub-reports. the following link will show you how to use datasets though;
Code Project - Creating Crystal Reports using C# with Datasets[^]
You need to add the following references - assuming Windows Forms;
CrystalDecisions.CrystalReports.Engine
CrystalDecisions.ReportSource
CrystalDecisions.Shared
CrystalDecisions.Windows.Forms
I am using VS 2015 with Crystal Reports 13.2 - refer to below link for runtime download;
Crystal Reports, Developer for Visual Studio Downloads - Business Intelligence (BusinessObjects) - SCN Wiki[^]
You will also need to add the following "usings"
using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared;
You have not specified which report - this is done as follows;
ReportDocument rptDoc = new ReportDocument(); rptDoc.Load("Put the full path and file name here");
Then loop through the Report Parameters and set them - I typically use the following;
if(rptDoc.DataDefinition.ParameterFields.Count > 0) { foreach(ParameterFieldDefinition crDef in rptDoc.DataDefinition.ParameterFields) { // Check for empty report name // Sub Reports will have a value, Main Report does not // Sub Report Parameters are passed by the Main Report if(crDef.ReportName == string.Empty) { object objValue = "What ever value I want to use"; rptDoc.SetParameterValue(crDef.ParameterFieldName, objValue); } } }
Next thing to do is create a Crystal reports ConnectionInfo, this is then passed to the Report
ConnectionInfo crConn = new ConnectionInfo(); crConn.ServerName = "My DB Server Name"; crConn.DatabaseName = "My Database Name"; crConn.UserID = "DB User Name"; crConn.Password = "DB Password"; // get the Report Tables Tables crTables = rptDoc.Database.Tables; for(int i = 0; i < crTables.Count; i++) { Table crTable = crTables[i]; TableLogOnInfo tblInfo = crTable.LogOnInfo; tblInfo.ConnectionInfo = crConn; crTable.ApplyLogOnInfo(tblInfo); } // then display the Report - here I am using a Crystal Reports Viewer in a Windows Form this.crystalReportsViewerFrm.ReportSource = rptDoc;
Good Luck
It's running example, I'm sharing with you:
Database method:
public DataSet SelectAll_() { var ds = new DataSet(); var conn = DACUtil.GetConnection(); try { SqlCommand cmd = new SqlCommand(SELECT_ALL, conn); var da = new SqlDataAdapter { SelectCommand = cmd }; da.Fill(ds); ds.Tables[0].TableName = "Employee"; ds.AcceptChanges(); } finally { conn?.Dispose(); } return ds; }
In ReportViewer.cs File:
public partial class ReportViewer : System.Web.UI.Page { ReportDocument _rpt = new ReportDocument(); EmployeeDAC employee = new EmployeeDAC(); protected void Page_Load(object sender, EventArgs e) { _rpt.Load(Server.MapPath("EmployeeReport.rpt")); DataSet ds = employee.SelectAll_(); _rpt.SetDataSource(ds); CrystalReportViewer1.ReportSource = _rpt; } }
For more detail, see the following article.
Crystal Report in Asp.net 2015 using C#[^]
这篇关于如何在Crystal C#中传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!