如何使用实体框架c#将数组中的文件插入SQL Server?

问题描述 投票:0回答:1

我想使用c#中的Entity Framework在数组中将文件插入SQL-Server。

这是我的数组:

public string[] FileArray = new string[20];

这是要插入数据库的代码,我在下面的代码中将数组放在哪里?

我将数组插入数据库的位置在哪里?

public bool SaveDB()
{
    try
    {
        HttpFileCollection File_Collection = Request.Files;
        using (DBEntitiesModelConn insert= new DBEntitiesModelConn())
        {
            if (IsPostBack)
            {
                foreach (string File_Uploader in File_Collection)
                {
                    HttpPostedFile Posted_File = File_Collection[File_Uploader];
                    if (Posted_File.ContentLength > 0)
                    {
                        BinaryReader Binary_Reader = new BinaryReader(Posted_File.InputStream);
                        byte[] File_Buffer = Binary_Reader.ReadBytes(Posted_File.ContentLength);
                        Binary_Reader = new BinaryReader(Posted_File.InputStream);
                        File_Buffer = Binary_Reader.ReadBytes(Posted_File.ContentLength);
                        fileNameWithoutExtension.ToString();

                        myServiceCP.LicenseApplicationAttachments.Add(new LicenseApplicationAttachment
                        {
                            FileName = fileNameWithoutExtension,
                            FileContentType = Posted_File.ContentType,
                            FileExtension = Path.GetExtension(Posted_File.FileName),
                            FileSize = Posted_File.ContentLength,
                            FileContent = File_Buffer
                        });
                    }
                }
                myServiceCP.SaveChanges();
            }
            else
            {
                return false;
            }
        }
    }
    catch
    {
        Exception ex;
    }
    return true;
}
c# arrays entity-framework bulkinsert
1个回答
0
投票

我假设您的20个文件名列表是File_Collection中文件的名称,其顺序与file_colelction中文件的顺序相同,在这种情况下,您将需要以下内容:

            for(int i = 0; i< FileArray.Length; i++){
            {
                HttpPostedFile Posted_File = File_Collection[i];
                if (Posted_File.ContentLength > 0)
                {
                    BinaryReader Binary_Reader = new BinaryReader(Posted_File.InputStream);
                    byte[] File_Buffer = Binary_Reader.ReadBytes(Posted_File.ContentLength);
                    Binary_Reader = new BinaryReader(Posted_File.InputStream);
                    File_Buffer = Binary_Reader.ReadBytes(Posted_File.ContentLength);

                    myServiceCP.LicenseApplicationAttachments.Add(new LicenseApplicationAttachment
                    {
                        FileName = FileArray[i],
                        FileContentType = Posted_File.ContentType,
                        FileExtension = Path.GetExtension(Posted_File.FileName),
                        FileSize = Posted_File.ContentLength,
                        FileContent = File_Buffer
                    });

但这是一个完整的猜测,因为您的问题缺少有关File_Collection,FileArray中的内容以及最终要实现的内容的详细信息

© www.soinside.com 2019 - 2024. All rights reserved.