c# – 如何使用SQL填充Crystal Reports

由于似乎没有任何方法可以在.Net中加载旧的VB6(ActiveReports)报告,我需要在.Net中重新创建数十个报告.我想以最不痛苦的方式做到这一点.

在VB6中,原始作者只为每个报告做了类似的事情:

adoConn.ConnectionString = globalConnectionObject.ConnectionString
adoConn.Source = ReportFunctions.GetAllUsers()

GetAllUsers()返回一个SQL字符串,用于选择一堆字段;然后在报告中使用这些字段.

现在:
我怎样才能在.Net中做类似的事情(使用内置的Crystal Reports或内置的“Microsoft报告技术”)?

我无法让“Database Expert”识别出globalConnectionObject(一个ADODB.Connection对象);如果我填写数据集并做

report.SetDataSource(dataSet)

它告诉我“报告没有表格.”

如何填充Crystal Reports报告?? (连接字符串/数据位置在编译时是未知的)

最佳答案 创建连接:

/// <summary>
/// Sets the connection.
/// </summary>
public void SetConnection()
{
    _connection = new SqlConnection(Settings.Default.connectionString);
    if (_connection.State == System.Data.ConnectionState.Open)
    {
        _connection.Close();
    }
    _connection.Open();
}

/// <summary>
/// Closes the connection.
/// </summary>
public void CloseConnection()
{
    if (_connection.State == System.Data.ConnectionState.Open)
    {
        _connection.Close();
        _connection.Dispose();
    }
}

使用上述连接手动填充数据集或向解决方案添加数据集以获取数据而无需编写连接代码.说你已经添加了Employee.XSD.在XSD中添加tableadapter,它将通过自动生成查询和数据表来帮助您从数据库中提取数据.完成所有这些事情之后.在项目的某处创建方法,

Public DataTable GetAllEmployees()
{
    Employee.EmployeeTableAdapter adapt = New Employee.EmployeeTableAdapter();
    DataTable dt = New DataTable();
    dt =    adapt.GetData();   // you can also use fill based on your criteria.
    return dt;   //your datatable with data
}

现在,在您的表单上添加一个reportviewer控件.

ReportViewer1 rpt = New ReportViewer();
ReportDocument rptDoc = new ReportDocument();
rptDoc.Load("path of rpt file");
rptDoc.SetDataSource(GetAllEmployees());
rpt.Document =  rptDoc;
rpt.Refresh();

在此之前从Crystal Report,根据您的要求在报表上添加表的字段.

另一种方式来实现

Crystal Reports可用于各种对象.如果您有动态绑定它的场景,请参阅下面的答案然后您可以做一件事,即向解决方案添加新数据集.创建数据表并添加具有适当数据类型的必需列.不要添加查询或表适配器.现在从您的代码中添加一个类文件并创建与datatable列完全相似的属性.现在将数据表设置为要报告的源,并在报表上添加其列.

For example , if you have columns
ID - integer
EmpName - string
Salary - double
Department - string

创建一个类

public class Employee
{
  private SqlConnection _connection;
  public int ID {get;set;}
  public string EmpName {get;set;}
  public double Salary {get;set;}
  public string Department  {get;set;}



/// <summary>
/// Sets the connection.
/// </summary>
public void SetConnection()
{
    //assuming connection string is placed in settings file from Project Properties.
    _connection = new SqlConnection(Settings.Default.connectionString);
    if (_connection.State == System.Data.ConnectionState.Open)
    {
        _connection.Close();
    }
    _connection.Open();
}

/// <summary>
/// Closes the connection.
/// </summary>
public void CloseConnection()
{
    if (_connection.State == System.Data.ConnectionState.Open)
    {
        _connection.Close();
        _connection.Dispose();
    }
}


  public DataTable GetEmployees()
  {
       DataTable dt = new DataTable("Employee");
       // using above connection
       SetConnection();
      using(SqlCommand command = new SqlCOmmand("commandText",_connection))
      {
         using(SqlDataReader reader = command.ExecuteReader())
         {
              dt.Load(reader);
         }
      }
       return dt;
  }
}

现在创建另一个函数来填充在dtataset中创建的数据表.

public void PopulateDataTable()
{
      DataTable dt = GetEmployee();
      Employee dsEmployee = New DataSet();
      dsEmployee.EmployeeDataTable dtEmp = new dsEmployee.EmployeeDataTable();
      dtEmp = dt;
}
点赞