c# - SSRS 自定义数据处理扩展错误

标签 c# reporting-services

我正在使用自定义数据处理扩展来调用存储过程。使用扩展在报表设计器中创建数据集时出现以下错误。我用 C# 编写了代码。

could not update a list of fields for the query. verify that you can connect to the data source and that your query syntax is correct.(Details-Object reference not set to an instance of an object.)

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ReportingServices.DataProcessing;
using System.Diagnostics;
using System.Text.RegularExpressions;


namespace DataBaseDPE
{  
    public class DBConnection:Microsoft.ReportingServices.DataProcessing.IDbConnectionExtension
    {
        private string mconnstring;
        private string localname = "Database Connection";
        private ConnectionState mState = ConnectionState.Open;


        public DBConnection()
        {
            Debug.WriteLine("DataSetConnection: Default Constructor");
        }
        public DBConnection(string Dconnection)
        {
            Debug.WriteLine("DataSetConnection Constructor overloaded with Connection String ");
            mconnstring = Dconnection;

        }

        public Microsoft.ReportingServices.DataProcessing.IDbTransaction BeginTransaction()
        {
            return (null);
        }


        public string ConnectionString
        {
            get
            {
                return mconnstring;

            }
            set
            {
                mconnstring = value;
            }
        }

        public int ConnectionTimeout
        {
            get
            {
                return 0;
            }
        }

        public ConnectionState State
        {
            get
            {
                return mState;
            }
        }

        public void Open()
        {
            mState = ConnectionState.Open;
            return;
        }


        public void Close()
        {
            mState = ConnectionState.Closed;
            return;
        }


        public Microsoft.ReportingServices.DataProcessing.IDbCommand CreateCommand()
        {
            return new DBCommand(this);
        }


        public string LocalizedName
        {
            get 
            { 
                return localname; 
            }
            set 
            { 
                localname = value; 
            }
        }

        public void SetConfiguration(string configuration)
        {
            try
            {
                SqlConnection sqlconn = new SqlConnection("Data Source=localhost;Initial Catalog=AdventureWorks2000;Integrated Security=True;");
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, null);
            }
        }
        public void Dispose()
        {

        }
        public string Impersonate
        { get; set; }

        public bool IntegratedSecurity
        { get; set; }

        public string Password
        { get; set; }

        public string UserName
        { get; set; }
    }

}

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ReportingServices.DataProcessing;
using System.Data.SqlClient;

namespace DataBaseDPE
{
    public class DBCommand : Microsoft.ReportingServices.DataProcessing.IDbCommand
    {
        DBConnection mconnection = null;
        private string mCmdText;
        private int mCmdTimeOut = 30;
        private CommandType CmdType;



        public DBCommand()
        {

        }
        public DBCommand(string CmdText)
        {
            mCmdText = CmdText;
        }
        public DBCommand(DBConnection aConnection)
        {
            mconnection = aConnection;
        }

        public void Cancel()
        {
            throw new NotImplementedException();
        }

        public string CommandText
        {
            get
            {
                return mCmdText;
            }
            set
            {
                mCmdText = value;
            }
        }

        public int CommandTimeout
        {
            get
            {
                return mCmdTimeOut;
            }
            set
            {
                mCmdTimeOut = value;
            }
        }

        public CommandType CommandType
        {
            get
            {
                return CmdType;
            }
            set
            {
                CmdType = value;
            }
        }

        public IDataParameter CreateParameter()
        {
            return (null);
        }

        public class MySqlDataReader:Microsoft.ReportingServices.DataProcessing.IDataReader
        {
            private System.Data.IDataReader sourceDataReader;
            private System.Data.DataTable dt;
            private System.Data.DataSet ds;
            private int fieldCount = 0;
            private string fieldName;
            private int fieldOrdinal;
            private Type fieldType;
            private object fieldValue;
            private int currentRow = 0;

            public MySqlDataReader(System.Data.IDataReader datareader)
            {
                this.sourceDataReader = datareader;
            }

            public MySqlDataReader(System.Data.DataTable dt)
            {
                // TODO: Complete member initialization

                this.dt = dt;

            }

            public MySqlDataReader(System.Data.DataSet ds)
            {
                // TODO: Complete member initialization
                this.ds = ds;
            }
            public int FieldCount
            {
                get
                {
                    fieldCount = ds.Tables[0].Columns.Count;
                    return fieldCount;
                }

            }

            public Type GetFieldType(int i)
            {
                fieldType =
               ds.Tables[0].Columns[i].DataType;
                return fieldType;
            }

            public string GetName(int i)
            {
                fieldName = ds.Tables[0].Columns[i].ColumnName;
                return fieldName;
            }

            public int GetOrdinal(string name)
            {
                fieldOrdinal =
               ds.Tables[0].Columns[name].Ordinal;
                return fieldOrdinal;
            }

            public object GetValue(int i)
            {
                fieldValue =
               ds.Tables[0].Rows[this.currentRow][i];
                return fieldValue;
            }

            public bool Read()
            {
                currentRow++;
                if (currentRow >= ds.Tables[0].Rows.Count)
                {
                    return (false);
                }
                else
                {
                    return (true);
                }
            }

            public void Dispose()
            {

            }

        }


        public IDataReader ExecuteReader(CommandBehavior behavior)
        {
            string query = "SampleSP";
            SqlConnection readerconn = new SqlConnection("Data Source=localhost;Initial Catalog=AdventureWorks2000;Integrated Security=SSPI");
            SqlCommand readercmd = new SqlCommand(query);


                readerconn.Open();
                readercmd = readerconn.CreateCommand();
                readercmd.CommandText = query;
                readercmd.CommandType = System.Data.CommandType.StoredProcedure;

                readerconn.Close();

                SqlDataAdapter adapter = new SqlDataAdapter(query,readerconn);

                readerconn.Open();
                adapter.SelectCommand = readercmd;

                System.Data.DataTable dt = new System.Data.DataTable();
                adapter.Fill(dt);
                System.Data.DataSet ds = new System.Data.DataSet();
                adapter.Fill(ds);                
                return new MySqlDataReader(ds);



        }

        public IDataParameterCollection Parameters
        {
            get { return (null); }
        }

        public IDbTransaction Transaction
        {
            get
            {
                return (null);
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public void Dispose()
        {

        }

    }
}

最佳答案

我得到了解决方案,在上面的代码中我没有实现 IDataParameter 和 IDataParameterCollection 接口(interface)。实现它们后,我的代码可以正常工作,现在我可以获取 SSRS 报告的数据集。

关于c# - SSRS 自定义数据处理扩展错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30053228/

相关文章:

performance - 快速查询在 SSRS 中运行缓慢

sql-server-2008 - SSRS格式日期从YYYYMMDD到MM/DD/YYYY

reporting-services - SSRS : Conditionally hiding columns based on parameter values - CSV export ignores

service - 用于格式化两位小数的 SSRS 表达式不显示零

c# - 保护 Azure Functions 使用的私钥

c# - 作为.NET初学者,我应该学习什么,在哪里可以找到开源项目?

c# - 添加项目后,字典在 ContainsKey 上返回 false

reporting-services - 报告定义 (RDL) 隐藏在平铺 View 中

c# - 在 C# 中使用 C++ DLL

c# - Entity Framework 6.0 原始 SQL 以查看模型