根据存储过程创建晶体报告

时间:2011-12-08 22:14:49

标签: c# .net stored-procedures dynamic crystal-reports

我想使用存储过程动态生成水晶报告。我使用RAS进程中的sdk。我已经用数据集创建了报告。 我正在使用的代码如下:

ISCRProcedure proc1 = new Procedure();
        CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo newConnectionInfo = new CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo();
        ISCRPropertyBag logonAttributes = new PropertyBag();
        PropertyBag connectionAttributes = new PropertyBag();
        logonAttributes.Add("Data Source", datasource);
        logonAttributes.Add("Initial Catalog", "Northwind");
        logonAttributes.Add("Provider", "SQLOLEDB");
        connectionAttributes.Add("Database DLL", "crdb_ado.dll");
        connectionAttributes.Add("QE_DatabaseType", "OLE DB (ADO)");
        connectionAttributes.Add("QE_LogonProperties", logonAttributes);
        connectionAttributes.Add("QE_SQLDB", true);
        connectionAttributes.Add("Server Name", servername);
        connectionAttributes.Add("SSO Enabled", false);
        newConnectionInfo.Attributes = connectionAttributes;
        newConnectionInfo.UserName = username;
        newConnectionInfo.Password = password;
        newConnectionInfo.Kind = CrConnectionInfoKindEnum.crConnectionInfoKindCRQE;
        proc1.ConnectionInfo = newConnectionInfo;
        proc1.Name = "sp_SelectAllOrders";
        oReportClientDocument.DatabaseController.AddTable(proc1);

我不知道如何访问存储过程的输出,以便稍后将其用作报告字段的数据源。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果您想看一个有效的例子,我有一个免费的C#类对象创建者,数据层创建者和存储过程生成器,位于: http://radstudio.codeplex.com

RAD Studio创建一个100%存储过程驱动的数据层;

下面的代码来自MS Application Block,但我上面的代码生成器下载包含了这个类的包装器,使其更容易;

 private static void FillDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType,
        string commandText, DataSet dataSet, string[] tableNames,
        params SqlParameter[] commandParameters)
    {
        if (connection == null) throw new ArgumentNullException("connection");
        if (dataSet == null) throw new ArgumentNullException("dataSet");

        // Create a command and prepare it for execution
        SqlCommand command = new SqlCommand();
        bool mustCloseConnection = false;
        PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);

        // Create the DataAdapter & DataSet
        using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
        {

            // Add the table mappings specified by the user
            if (tableNames != null && tableNames.Length > 0)
            {
                string tableName = "Table";
                for (int index = 0; index < tableNames.Length; index++)
                {
                    if (tableNames[index] == null || tableNames[index].Length == 0) throw new ArgumentException("The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames");
                    dataAdapter.TableMappings.Add(tableName, tableNames[index]);
                    tableName += (index + 1).ToString();
                }
            }

            // Fill the DataSet using default values for DataTable names, etc
            dataAdapter.Fill(dataSet);

            // Detach the SqlParameters from the command object, so they can be used again
            command.Parameters.Clear();
        }

        if (mustCloseConnection)
            connection.Close();
    }