填充:派生的iDBCommand上的SelectCommand.Connection错误

时间:2016-12-17 11:05:27

标签: c# sqlconnection sqlclient

在您进行此操作之前,请先查看。我知道这里有很多线程有这个错误消息,但我还没找到一个有这个特殊问题的人。我已经创建了一个实现IDbCommand接口的自定义命令类,如下所示:

internal class Prozedur : IDbCommand
    {
        private static SqlCommand command = new SqlCommand() ;
        private SqlConnection connection;
        string commandtext;
        CommandType commandType;

        public Prozedur(string abfrage)
        {
            commandtext = abfrage;

            commandType = CommandType.StoredProcedure;
            connection = (SqlConnection)new Verbindung();
            SqlCommand command = new SqlCommand(commandtext, connection);

        }

        public static explicit operator SqlCommand(Prozedur v)
        {
            return command;
        }
    ...

我正在使用这样的对象:

    internal class Tabelle : DataTable
    {
        DataTable tabelle = new DataTable();

        internal Tabelle(string abfrage)
        {
            Prozedur p = new Prozedur(abfrage);

            SqlDataAdapter adapter = new SqlDataAdapter((SqlCommand)p);
            adapter.Fill(tabelle);
        }
    }

然而,在adapter.Fill(tabelle)我得到错误“Fill:SelectCommand.Connection属性尚未初始化。”。但是,如果我看对象p,连接就在那里:

enter image description here

更新

如果我改变这样的代码

    internal class Tabelle : DataTable
    {
        DataTable tabelle = new DataTable();

        internal Tabelle(string abfrage)
        {
            Prozedur p = new Prozedur(abfrage);
            SqlCommand c = (SqlCommand)p;
            //added this line
            c.Connection = p.Connection;

            SqlDataAdapter adapter = new SqlDataAdapter((SqlCommand)p);

            adapter.Fill(tabelle);
        }
    }

我收到p.Connection; 的编译错误C#无法隐式转换为。存在显式转换(您是否错过了演员?)

连接的代码是:

        public IDbConnection Connection
        {
            get
            {

                return connection;
            }

            set
            {
                connection = (SqlConnection)value;
            }
        }

1 个答案:

答案 0 :(得分:0)

问题出在转换方法中。当我更改下面的代码时,它的工作原理。基本上我必须使所有变量都是静态的,并在convert方法中返回一个新的SqlCommand对象。

 internal class Prozedur : IDbCommand
    {
        private static SqlCommand command = new SqlCommand();
        private static SqlConnection connection;
        static string commandtext;
        CommandType commandType;

        public Prozedur(string abfrage)
        {
            commandtext = abfrage;
            commandType = CommandType.StoredProcedure;
            connection = (SqlConnection)new Verbindung();
        }

        public static explicit operator SqlCommand(Prozedur v)
        {
            return new SqlCommand() { CommandText = commandtext, Connection = connection, CommandType = command.CommandType };
        }
...

它没有静态变量就像这样:

    internal class Prozedur : IDbCommand
    {
        private SqlCommand command = new SqlCommand();
        private SqlConnection connection;
        string commandtext;
        CommandType commandType;

        public Prozedur(string abfrage)
        {
            commandtext = abfrage;
            commandType = CommandType.StoredProcedure;
            connection = (SqlConnection)new Verbindung();
        }

        public static explicit operator SqlCommand(Prozedur v)
        {
            return new SqlCommand() { CommandText = v.CommandText, Connection = (SqlConnection)v.Connection, CommandType = v.CommandType };
        }
...