OracleCommand.Executenonquery抛出System.InvalidOperationException'

时间:2015-08-14 05:51:36

标签: c# sql oracle

以下函数抛出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)

2 个答案:

答案 0 :(得分:1)

问题不在于Query(查询错误会引发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();

答案 1 :(得分:0)

您从未将Command对象与Connection对象相关联。

您应该将参数传递给此函数并在此处创建命令。然后,您可以在命令的构造函数中传递连接。

将外部实例(函数参数)放入using块应该从一开始就引发很多红色标记。如果可以,您应该在using块中创建Disposable对象。使用块的目的是确保它被处理掉,如果你在块之前构造对象,你的覆盖范围就会有差距。