我不知道如何调试。我试图将文件上传到我的数据库中(通过将二进制文件内容读入我的存储过程)。我传递的输入流,以我的方法,像这样:为什么bytes.length = 0?
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.
谢谢。
乔恩,请给我一个样品;我在C#中阅读文件有点新。谢谢。 – user1477388 2013-02-20 15:41:35
@ user1477388:我建议你花一段时间尝试一下,然后将该代码编辑到问题中。你会学到更多的方式。 (请参阅Stream.Read的文档以获得一个起点。) – 2013-02-20 15:42:37
Jon,我接受了您的建议,并尝试自行研究此问题。但是,我收到错误'无效的文本,ntext或图像指针值0x00000000000000000000000000000000.'。我发布了我的整个代码,你认为你可能没有什么问题? – user1477388 2013-02-20 17:36:08