OracleCommand.Executenonquery throws 'System.InvalidOper
本文关键字:System InvalidOper Executenonquery throws OracleCommand | 更新日期: 2023-09-27 18:07:57
下面的函数抛出System.InvalidOperationException
:
internal void executeNonQuery(string connectionString, OracleCommand cmd)
{
using (OracleConnection conn = new OracleConnection(connectionString))
{
using (cmd)
{
conn.Open();
cmd.ExecuteNonQuery(); //here is the error
conn.Close();
}
}
}
附加信息为:
由于对象的当前状态,操作无效。
我尝试在表中插入一行。有其他方法来做这个或修复这个错误吗?
编辑:我使用以下方法在binaryManager类中构建查询:
internal object[] binaryInsert(string tblName, string tblQuery, int conStrgID, int cq)
{
object[] retValues = new object[3];
Stream myStream = null ;
OracleConnection con = null;
string conString = qm.getConnectionString("ConnectionStringToMyDB"); //is correct
byte[] data = GetBytes(tblQuery);
String sql = "INSERT INTO MYTABLES VALUES (NULL, '" + tblName + "', ':tblQueryBlob', " + conStrgID + ", " + cq + ")";
OracleCommand cmd = new OracleCommand();
cmd.CommandText = sql; // Set the sql-command
cmd.Connection = con; //con is an OracleConnection
OracleParameter param = cmd.Parameters.Add("tblQueryBlob", OracleDbType.Blob); //Add the parameter for the blobcolumn
param.Direction = ParameterDirection.Input;
param.Value = data; //Asign the Byte Array to the parameter
//command containts the parameter :tblQueryBlob with its value
retValues[0] = cmd;
retValues[1] = conString;
return retValues;
}
private byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
我用下面的代码从另一个类调用binaryInsert方法:
BinaryManager bm = new bBinaryManager();
string sqlQuery = "large string with 5100 characters";
object[] binaryValues = bm.binaryInsert("TextTbl", sqlQuery, 1, 1);
string conString = binaryValues[1].ToString();
OracleCommand cmd = (OracleCommand)binaryValues[0];
QueryManager qm = new QueryManager();
qm.executeNonQuery(conString, cmd);
EDIT2 (ExecuteReader):
int csID = 1;
using (OracleConnection conn = new OracleConnection(conString))
{
using (OracleCommand myCommand = new OracleCommand(query, conn))
{
conn.Open();
myCommand.Connection = conn; // FIX!
using(OracleDataReader read = myCommand.ExecuteReader())
{
if (read.HasRows)
{
while (read.Read())
{
if (read[0] == System.DBNull.Value)
{
TableObject tableDBNull = new TableObject(csID, Convert.ToString(read[1]), (byte[])read[2], Convert.ToInt32(read[3]), Convert.ToInt32(read[4]));
allTables.Add(tableDBNull);
csID++;
}
else
{
TableObject tableDBNNull = new TableObject(Convert.ToInt32(read[0]), Convert.ToString(read[1]), (byte[])read[2], Convert.ToInt32(read[3]), Convert.ToInt32(read[4]));
allTables.Add(tableDBNNull);
}
}
}
}
conn.Close();
}
if块出错:if (read[0] == System.DBNull.Value)
问题不在于查询(查询错误会从Oracle命名空间引发异常)。
在您的示例中,该命令与任何连接都不相关。
你可以用一些方法来解决它,例如
internal void executeNonQuery(string connectionString, OracleCommand cmd)
{
using (OracleConnection conn = new OracleConnection(connectionString))
{
using (cmd)
{
conn.Open();
cmd.Connection = conn; // FIX!
cmd.ExecuteNonQuery(); //here is the error
conn.Close();
}
}
}
通常我更喜欢另一种方式
OracleConnection con = new OracleConnection(connectionString);
// Creating command from the connection
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = ...........
cmd.ExecuteNonQuery();
您从未将Command
对象连接到Connection
对象。
你应该把你的参数传递给这个函数并在这里创建一个命令。然后,您可以在命令的构造函数中传递您的连接。
将外部实例(函数参数)放入using块中会从一开始就引发很多危险信号。如果可以,应该在using块中创建Disposable对象。using块的目的是确保它被处理,如果在块之前构造对象,则在覆盖范围中存在空白。