1

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

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.

谢谢你。

4

2 回答 2

4

它表明您已经到达流的末尾。从文档中:

当从 ReadBytes 返回的 Byte 数组的长度为零时,检测到正在读取的文件的结尾。

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

于 2013-02-20T15:08:37.580 回答
0

我添加了这段代码,现在它工作正常:

IDbConnection con = trn.Connection; 使用 (IDbCommand cmd = con.CreateCommand()) { cmd.Transaction = trn;

            cmd.CommandText = "UPDATE ATTACHMENTS_CONTENT SET ATTACHMENT = @BYTES Where ID=@ID";

            // 2. define parameters used in command object
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@BYTES";
            param.Value = bytes;
            cmd.Parameters.Add(param);

            // 2. define parameters used in command object
            SqlParameter param2 = new SqlParameter();
            param2.ParameterName = "@ID";
            param2.Value = gAttachmentContentID;
            cmd.Parameters.Add(param2);

            cmd.ExecuteNonQuery();
        }
于 2013-02-20T18:48:56.970 回答