c# - 为什么 bytes.length = 0?

标签 c# asp.net binary webforms

我不确定如何调试它。我正在尝试将文件上传到我的数据库(通过将二进制文件内容读入我的存储过程)。我像这样将输入流传递给我的方法:

LoadFile(gAttachmentContentID, file.InputStream, trn);

这是我的方法:

    public static void LoadFile(Guid gAttachmentContentID, Stream stm, IDbTransaction trn)
            {
                const int BUFFER_LENGTH = 40 * 1024;
                byte[] binFILE_POINTER = new byte[32];
                //Testing check out check in
                // 01/20/2006 Paul.  Must include in transaction
                SqlProcs.spATTACHMENTS_CONTENT_InitPointer(gAttachmentContentID, ref binFILE_POINTER, trn);

                using (BinaryReader reader = new BinaryReader(stm))
                {
                    int nFILE_OFFSET = 0;
                    byte[] binBYTES = reader.ReadBytes(BUFFER_LENGTH);
                    Debug.Print("binBYTES=" + binBYTES.Length.ToString());
                    while (binBYTES.Length > 0)
                    {
                        // 08/14/2005 Paul.  gID is used by Oracle, binFILE_POINTER is used by SQL Server. 
                        // 01/20/2006 Paul.  Must include in transaction
                        SqlProcs.spATTACHMENTS_CONTENT_WriteOffset(gAttachmentContentID, binFILE_POINTER, nFILE_OFFSET, binBYTES, trn);
                        nFILE_OFFSET += binBYTES.Length;
                        binBYTES = reader.ReadBytes(BUFFER_LENGTH);
                    }
                }
            }

为什么 binBYTES.length = 0?我该如何调试这种类型的东西?

编辑:

我现在正在使用这个功能:

    public static void LoadFile2(Guid gAttachmentContentID, Stream stm, IDbTransaction trn)
    {

        stm.Position = 0;
        byte[] binFILE_POINTER = new byte[32];

        // Now read s into a byte buffer. 
        byte[] bytes = new byte[stm.Length];
        int numBytesToRead = (int)stm.Length;
        int numBytesRead = 0;
        while (numBytesToRead > 0)
        {
            // Read may return anything from 0 to 10. 
            int n = stm.Read(bytes, numBytesRead, 10);
            // The end of the file is reached. 
            if (n == 0)
            {
                break;
            }
            numBytesRead += n;
            numBytesToRead -= n;



        }
        stm.Close();
        SqlProcs.spATTACHMENTS_CONTENT_WriteOffset(gAttachmentContentID, binFILE_POINTER, 0, bytes, trn);
        // numBytesToRead should be 0 now, and numBytesRead should 
        // equal 100.
        Console.WriteLine("number of bytes read: {0:d}", numBytesRead);

    }

通过这个过程:

public static void spATTACHMENTS_CONTENT_WriteOffset(Guid gID, byte[] binFILE_POINTER, Int32 nFILE_OFFSET, byte[] byBYTES, IDbTransaction trn)
        {
            IDbConnection con = trn.Connection;
            using ( IDbCommand cmd = con.CreateCommand() )
            {
                cmd.Transaction = trn;
                cmd.CommandType = CommandType.StoredProcedure;
                if ( Sql.IsOracle(cmd) )
                    cmd.CommandText = "spATTACHMENTS_CONTENT_WriteOff";
                else
                    cmd.CommandText = "spATTACHMENTS_CONTENT_WriteOffset";
                IDbDataParameter parID               = Sql.AddParameter(cmd, "@ID"              , gID                );
                IDbDataParameter parFILE_POINTER     = Sql.AddParameter(cmd, "@FILE_POINTER"    , binFILE_POINTER    );
                IDbDataParameter parMODIFIED_USER_ID = Sql.AddParameter(cmd, "@MODIFIED_USER_ID",  Security.USER_ID  );
                IDbDataParameter parFILE_OFFSET      = Sql.AddParameter(cmd, "@FILE_OFFSET"     , nFILE_OFFSET       );
                IDbDataParameter parBYTES            = Sql.AddParameter(cmd, "@BYTES"           , byBYTES            );
                cmd.ExecuteNonQuery();
            }
        }

但是我得到以下错误:

Invalid text, ntext, or image pointer value 0x00000000000000000000000000000000.

谢谢。

最佳答案

它表明您已经到达流的末尾。来自documentation :

The end of the file that is being read is detected when the length of the Byte array returned from ReadBytes is zero.

就我个人而言,我根本不会在这里创建 BinaryReader:我会创建一个单个 字节数组,您可以重复使用它,然后调用 Stream .Read 重复(并复制数据)直到返回 0 以指示它已到达流的末尾。

关于c# - 为什么 bytes.length = 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14983091/

相关文章:

c# - (更多)线程二叉树中旋转节点时的高效锁定

ASP.NET SqlServer session 复制

iphone - 创建应用程序的免费版本,但将应用程序单独放在调试设备上

将 uint8_t 十六进制值转换为二进制

c# - 查找 .NET 规范

c# - 如何避免在 RX 中使用 Subjects

c# - IIS 中托管的 tcp 端点的 WCF 路由

asp.net - 如何在asp.net中生成随机唯一的16位数字而不发生冲突

c# - Web 应用程序中的确认消息框

python - 使用 pyserial 和 python 3.4 通过串行发送二进制文件