首頁 >後端開發 >C++ >將檔案作為位元組數組儲存在資料庫中是最有效的方法嗎?

將檔案作為位元組數組儲存在資料庫中是最有效的方法嗎?

Susan Sarandon
Susan Sarandon原創
2025-01-15 09:31:48504瀏覽

Is Storing Files as Byte Arrays in a Database the Most Efficient Method?

將任何格式的檔案儲存為位元組數組

將各種格式的檔案儲存到資料庫或磁碟需要仔細考慮。一種常見的方法是在儲存之前將檔案轉換為位元組數組。此方法允許儲存任何格式的文件,但它提出了一個問題,即它是否是最佳方法。

為了說明這個過程,請考慮答案中提供的以下程式碼片段:

將檔案作為位元組陣列插入 SQL Server 資料庫:

<code class="language-c#">public static void databaseFilePut(string varFilePath) {
    byte[] file;
    using (var stream = new FileStream(varFilePath, FileMode.Open, FileAccess.Read)) {
        using (var reader = new BinaryReader(stream)) {
            file = reader.ReadBytes((int) stream.Length);       
        }          
    }
    using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
    using (var sqlWrite = new SqlCommand("INSERT INTO Raporty (RaportPlik) Values(@File)", varConnection)) {
        sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
        sqlWrite.ExecuteNonQuery();
    }
}</code>

從資料庫擷取檔案作為位元組數組:

<code class="language-c#">public static MemoryStream databaseFileRead(string varID) {
    MemoryStream memoryStream = new MemoryStream();
    using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
    using (var sqlQuery = new SqlCommand(@"SELECT [RaportPlik] FROM [dbo].[Raporty] WHERE [RaportID] = @varID", varConnection)) {
        sqlQuery.Parameters.AddWithValue("@varID", varID);
        using (var sqlQueryResult = sqlQuery.ExecuteReader())
            if (sqlQueryResult != null) {
                sqlQueryResult.Read();
                byte[] blob = new byte[(int)sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue)];
                sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
                memoryStream.Write(blob, 0, blob.Length);
            }
    }
    return memoryStream;
}</code>

使用 MemoryStream 將檔案作為位元組數組插入資料庫:

<code class="language-c#">public static int databaseFilePut(MemoryStream fileToPut) {
        int varID = 0;
        byte[] file = fileToPut.ToArray();
        const string preparedCommand = @"
                    INSERT INTO [dbo].[Raporty]
                               ([RaportPlik])
                         VALUES
                               (@File)
                        SELECT [RaportID] FROM [dbo].[Raporty]
            WHERE [RaportID] = SCOPE_IDENTITY()
                    ";
        using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
        using (var sqlWrite = new SqlCommand(preparedCommand, varConnection)) {
            sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;

            using (var sqlWriteQuery = sqlWrite.ExecuteReader())
                while (sqlWriteQuery != null && sqlWriteQuery.Read()) {
                    varID = sqlWriteQuery["RaportID"] is int ? (int) sqlWriteQuery["RaportID"] : 0;
                }
        }
        return varID;
    }</code>

這些程式碼片段示範了將檔案轉換為位元組數組的過程,然後將其儲存在 VARBINARY(MAX) 類型的資料庫列中。需要時,可以將檔案檢索為位元組數組,並儲存為新檔案或串流傳輸到應用程式中。

The provided text is already quite concise and well-written. The primary change made is to correct a minor issue in the databaseFileRead function where the byte array length was not co🎜> function where the byte array length was not correctly cast to an int. were deemed necessary for effective paraphrasing while maintaining the original meaning and structure.

以上是將檔案作為位元組數組儲存在資料庫中是最有效的方法嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn