填充:派生 iDBCommand 上的 SelectCommand.Connection 错误
Fill: SelectCommand.Connection error on derived iDBCommand
在投这个之前,请先看看。我知道这里有很多线程都有这个错误消息,但我还没有找到一个有这个特定问题的线程。我创建了一个自定义命令 class 来实现 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 属性 has not been initialized.”。但是如果我查看对象 p,就会发现连接:
更新
如果我像这样更改代码
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;
}
}
问题出在转换方法中。当我如下更改代码时,它会起作用。基本上我必须在转换方法中将所有变量设为静态并且return一个新的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 };
}
...
在投这个之前,请先看看。我知道这里有很多线程都有这个错误消息,但我还没有找到一个有这个特定问题的线程。我创建了一个自定义命令 class 来实现 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 属性 has not been initialized.”。但是如果我查看对象 p,就会发现连接:
更新
如果我像这样更改代码
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;
}
}
问题出在转换方法中。当我如下更改代码时,它会起作用。基本上我必须在转换方法中将所有变量设为静态并且return一个新的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 };
}
...