无法在 C# 中将 byte[] 数组数据保存到数据库.它节

Can#39;t save byte[] array data to database in C#. It#39;s saving 0x(无法在 C# 中将 byte[] 数组数据保存到数据库.它节省了 0x)
本文介绍了无法在 C# 中将 byte[] 数组数据保存到数据库.它节省了 0x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传文件并将其二进制内容保存到我的数据库字段.每次我尝试保存时,它只是将数据库输入为 0x.

I am trying to upload a file and save it's binary content to my database field. Each time I try to save, it simply enters the database as 0x.

相关代码如下:

<form id="frmDefault" enctype="multipart/form-data" runat="server">
    <asp:FileUpload ID="fileInput" runat="server" style="margin: 8px 0 13px 0;" /><br />
    <asp:textbox rows="5" TextMode="multiline" runat="server" id="description" /><br />
    <asp:Button ID="btnUpload" Text="Upload File" runat="server" 
        onclick="btnUpload_Click" />
</form>

我建立变量:

HttpPostedFile file = fileInput.PostedFile;

而且,它命中了这个函数:

And, it hits this function:

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);

                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);
                }
            }
}

如何读取正确的内容以将整个文件保存到数据库中?

How can I read the correct contents in order to save the entire file to the database?

谢谢.

    #region spATTACHMENTS_CONTENT_WriteOffset
    /// <summary>
    /// spATTACHMENTS_CONTENT_WriteOffset
    /// </summary>
    public static void spATTACHMENTS_CONTENT_WriteOffset(Guid gID, byte[] binFILE_POINTER, Int32 nFILE_OFFSET, byte[] byBYTES)
    {
        DbProviderFactory dbf = DbProviderFactories.GetFactory();
        using ( IDbConnection con = dbf.CreateConnection() )
        {
            con.Open();
            using ( IDbTransaction trn = con.BeginTransaction() )
            {
                try
                {
                    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();
                    }
                    trn.Commit();
                }
                catch(Exception ex)
                {
                    trn.Rollback();
                    throw(new Exception(ex.Message, ex.InnerException));
                }
            }
        }
    }
    #endregion

推荐答案

我也遇到了同样的问题,就是第一次在数据库端正确保存了图片,但是如果随后验证失败,然后我尝试输入有效数据后再次保存图像我会在图像列中得到 0x .为了解决这个问题,我做了什么@Ann L. 说:

I was having the same problem, that is, the first time the image was saved correctly on the database side, but if subsequently validation failed and then I tried to save the image again after entering valid data I would get 0x in the image column. To solve that I did what @Ann L. said:

byte[] photo = null;

if(model.Photo != null)
{
    var stream = model.Photo.InputStream;
    stream.Position = 0;

    using(BinaryReader br = new BinaryReader(model.Photo.InputStream))
    {
        photo = br.ReadBytes(model.Photo.ContentLength);
    }
}

这篇关于无法在 C# 中将 byte[] 数组数据保存到数据库.它节省了 0x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)