Using 块中的 OleDbCommand:无法使用与其底层 RCW 分离的 COM 对象
OleDbCommand in Using block: COM object that has been separated from its underlying RCW cannot be used
仅当我使用 parameters
在 using
块中执行 OleDbCommand
对象时才出现错误 "System.Runtime.InteropServices.InvalidComObjectException: 'COM object that has been separated from its underlying RCW cannot be used.'"
using (OleDbCommand comm = new OleDbCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.Text;
string txt = "SELECT* FROM [ListForGroup] WHERE [email] = @email";
comm.Parameters.Add("@email", OleDbType.VarWChar).Value = userEmail;
comm.CommandText = txt;
conn.Open();
OleDbDataReader reader = comm.ExecuteReader();
List<ListForGroups> list = returnLists(reader);
conn.Close();
cmbSelectList.DataSource = list;
cmbSelectList.DisplayMember = "listName";
cmbSelectList.ValueMember = "listForGroupsID";
}
有谁知道这是为什么。我可以解决它使用 OleDbCommand
对象与 CommandText
没有 parameters
但我读到这是个坏主意。我想在 using
块中进行,以确保所有资源都将被释放。
我解决了这个问题
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand comm = new OleDbCommand();
comm.Connection = conn;
comm.CommandType = CommandType.Text;
string txt = "SELECT* FROM [ListForGroup] WHERE [email] = @email";
comm.Parameters.Add("@email", OleDbType.VarWChar).Value = userEmail;
comm.CommandText = txt;
conn.Open();
OleDbDataReader reader = comm.ExecuteReader();
List<ListForGroups> list = returnLists(reader);
cmbSelectList.DataSource = list;
cmbSelectList.DisplayMember = "listName";
cmbSelectList.ValueMember = "listForGroupsID";
}
我不知道为什么我没有在 using
旁边的括号中创建 OleDbConnection
。另一个解决它的时间是在 using
块之后关闭 connection
。
仅当我使用 parameters
using
块中执行 OleDbCommand
对象时才出现错误 "System.Runtime.InteropServices.InvalidComObjectException: 'COM object that has been separated from its underlying RCW cannot be used.'"
using (OleDbCommand comm = new OleDbCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.Text;
string txt = "SELECT* FROM [ListForGroup] WHERE [email] = @email";
comm.Parameters.Add("@email", OleDbType.VarWChar).Value = userEmail;
comm.CommandText = txt;
conn.Open();
OleDbDataReader reader = comm.ExecuteReader();
List<ListForGroups> list = returnLists(reader);
conn.Close();
cmbSelectList.DataSource = list;
cmbSelectList.DisplayMember = "listName";
cmbSelectList.ValueMember = "listForGroupsID";
}
有谁知道这是为什么。我可以解决它使用 OleDbCommand
对象与 CommandText
没有 parameters
但我读到这是个坏主意。我想在 using
块中进行,以确保所有资源都将被释放。
我解决了这个问题
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand comm = new OleDbCommand();
comm.Connection = conn;
comm.CommandType = CommandType.Text;
string txt = "SELECT* FROM [ListForGroup] WHERE [email] = @email";
comm.Parameters.Add("@email", OleDbType.VarWChar).Value = userEmail;
comm.CommandText = txt;
conn.Open();
OleDbDataReader reader = comm.ExecuteReader();
List<ListForGroups> list = returnLists(reader);
cmbSelectList.DataSource = list;
cmbSelectList.DisplayMember = "listName";
cmbSelectList.ValueMember = "listForGroupsID";
}
我不知道为什么我没有在 using
旁边的括号中创建 OleDbConnection
。另一个解决它的时间是在 using
块之后关闭 connection
。