简体   繁体   English

给定ExecuteNonQuery()中对象的当前状态,此操作无效

[英]Invalid operation given the current state of the object in ExecuteNonQuery()

I'm trying to insert and in which case there is a duplicate key update. 我正在尝试插入,在这种情况下,存在重复的密钥更新。 Everything is from Oracle and using a procedure but I get this error and I do not know why 一切都来自Oracle,并使用一个过程,但是出现此错误,我不知道为什么

public static int GrabarJugador(Jugador_E jugador)
    {
        int respuesta = 0;

        try
        {
            conexion = bd.LeerDeBaseDeDatos();

            orden = new OracleCommand();
            orden.CommandText = "CONSULTAS.grabar_jugador";
            orden.CommandType = CommandType.StoredProcedure;
            orden.Parameters.Add("v_nombre", OracleDbType.Varchar2, 60).Value = jugador.Nombre;
            orden.Parameters.Add("v_equipo", OracleDbType.Varchar2, 50).Value = jugador.EquipoJugador;
            orden.Parameters.Add("v_direccion", OracleDbType.Varchar2, 150).Value = jugador.Direccion;
            orden.Parameters.Add("v_puesto", OracleDbType.Varchar2, 2).Value = jugador.PuestoHab;
            orden.Parameters.Add("v_fec_na", OracleDbType.Date).Value = jugador.Fecha;
            orden.Parameters.Add("v_foto", OracleDbType.Blob).Value = jugador.Foto;

            respuesta = orden.ExecuteNonQuery();

            orden.Dispose();

            bd.CerrarConexion();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error " + e.ToString());
            Console.ReadLine();
        }

            return respuesta;
    }

The code procedure in DB Oracle is this: DB Oracle中的代码过程是这样的:

PROCEDURE GRABAR_JUGADOR (v_nombre         VARCHAR,
                          v_equipo         VARCHAR2,
                          v_direccion      VARCHAR2,
                          v_puesto_h       VARCHAR2,
                          v_fec_na         DATE,
                          v_foto        IN BLOB)
IS
BEGIN
    INSERT INTO JUGADOR (NOMBRE,
                         DIRECCION,
                         PUESTO_HAB,
                         FECHA_NAC,
                         EQUIPO_JUGADOR,
                         FOTO_JUGADOR)
         VALUES (v_nombre,
                 v_direccion,
                 v_puesto_h,
                 v_fec_na,
                 v_equipo,
                 v_foto);
EXCEPTION
    WHEN DUP_VAL_ON_INDEX
    THEN
        UPDATE jugador
           SET equipo_jugador = v_equipo,
               direccion = v_direccion,
               puesto_hab = v_puesto_h,
               fecha_nac = v_fec_na,
               foto_jugador = v_foto
         WHERE nombre = v_nombre;

  WHEN OTHERS
    THEN
        raise_application_error (
            -20100,
            'Error inexperado: '|| SQLERRM);
END GRABAR_JUGADOR;

I tried if the error is for Blob or Date but isn't this. 我试过错误是否是Blob或Date,但这不是。 The error comes out just in Orden.ExecuteNonQuery(); 错误仅在Orden.ExecuteNonQuery();中出现。

EDIT 1: I repair the error, was the conection in command, but now when i insert/update the picture player (blob) when I charge the photo the program give me 1 error: The parameter is not valid. 编辑1:我修复了错误,是命令中的连接,但是现在当我为照片充电时插入/更新图片播放器(blob)时,程序给我1错误:该参数无效。

EDIT 2: I have seen that the error is when I save the blob in the Oracle DB 编辑2:我已经看到错误是当我将blob保存在Oracle DB中时

if (jugador.Foto != null)
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(jugador.Foto, 0, Convert.ToInt32(jugador.Foto.Length));
            Bitmap bm = new Bitmap(ms, false); //this line break error
            ms.Dispose();
            pictureBox1.Image = bm;
        }

Thanks for help me. 谢谢你帮我

Since you state that you've solved your first issue, I'll just address the bottom one (you should probably edit your question to just be that, since the first part is irrelevant now). 既然您说您已经解决了第一个问题,那么我将只解决最底层的问题(您可能应该将您的问题编辑为那样,因为第一部分现已不相关)。

The issue you're seeing is because you write to the end of the stream and then try to read the image with the end of the stream as the starting point . 您看到的问题是因为您写入流的末尾,然后尝试以流的末尾为起点读取图像。 You need to move back to the start of the stream before you can read the image. 您需要先移回流的开头,然后才能阅读图像。 Your code therefore should be: 因此,您的代码应为:

if (jugador.Foto != null)
{
    using(MemoryStream ms = new MemoryStream())
    {
        ms.Write(jugador.Foto, 0, Convert.ToInt32(jugador.Foto.Length));
        ms.Seek(0, SeekOrigin.Begin); // seek to the beginning of the stream
        Bitmap bm = new Bitmap(ms, false);
        pictureBox1.Image = bm;
    }
}

I've also moved the MemoryStream into a using block. 我也将MemoryStream移到了using块中。

Tangential things to note: 切线注意事项:

  • Microsoft's implementation of MemoryStream means that this isn't a problem, but if you were writing to a file, or a network socket, etc. you may have to call .Flush() to ensure that the data has been sent/saved and isn't still just stuck in a temporary memory store. 微软对MemoryStream的实现意味着这不是问题,但是如果您正在写入文件或网络套接字等,则可能必须调用.Flush()来确保数据已发送/保存.Flush()存在仍然不仅仅停留在临时存储中。
  • Not all Streams are seekable, and thus have a CanSeek property (in addition to CanRead and CanWrite ) to indicate if you can seek. 并非所有Stream都是可搜索的,因此具有CanSeek属性(除了CanReadCanWrite )可指示您是否可以搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM