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

时间:2015-05-05 12:39:57

标签: c# reporting-services

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

  

无法更新查询的字段列表。验证你可以   连接到数据源,您的查询语法是   正确。(详细信息 - 对象引用未设置为。的实例   对象。)

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()
        {

        }

    }
}

1 个答案:

答案 0 :(得分:1)

我得到了解决方案,在上面的代码中我没有实现IDataParameter和IDataParameterCollection接口。在实现它们之后,我的代码正在运行,现在我能够获得SSRS报告的数据集。