Get the App
SLTechnology News&Howtos  ›  Development  › 

How to save a file to a database as a binary stream

Shulou Source: shulou.com Published: 2022-06-03 05:53:15 09月28日 Update

This article is about how to save a file to a database as a binary stream. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Write the file to the database in binary stream format

First get the file path, then save the file as binary read out in a binary array, establish a connection with the database, assign the binary array to the corresponding parameters in the SQL statement, and complete the operation of writing the file to the database.

The copy code is as follows:

/ / write file streams to the database

/ / /

/ / the path to the database file

/ / the line identifier ID of the file inserted in the database

/ / /

Public int UploadFile (string filePath, string id)

{

Byte [] buffer = null

Int result = 0

If (! string.IsNullOrEmpty (filePath))

{

String file = HttpContext.Current.Server.MapPath (filePath)

Buffer = File.ReadAllBytes (file)

Using (SqlConnection conn = new SqlConnection (DBOperator.ConnString))

{

Using (SqlCommand cmd = conn.CreateCommand ())

{

Cmd.CommandText = "update DomesticCompanyManage_Main_T set ZBDocumentFile = @ fileContents where MainID ='" + id + "'"

Cmd.Parameters.AddRange (new [] {

New SqlParameter (@ fileContents, buffer)

});

Conn.Open ()

Result = cmd.ExecuteNonQuery ()

Conn.Close ()

}

}

Return result

}

Else

Return 0

}

2. Read the file from the database and establish the file in the corresponding format

To read a file from the database, just create the corresponding file according to the required path, and then write the binary stream stored in the database to the newly created file.

If there is a file with the same name in this directory, the original file will be overwritten

The copy code is as follows:

/ / read the file stream from the database

/ / shipmain.Rows [0] ["ZBDocument"], the full path to the file

/ / shipmain.Rows [0] ["ZBDocumentFile"], the file stream stored in the database

If (shipmain.Rows [0] ["ZBDocumentFile"]! = DBNull.Value)

{

Int arraySize = ((byte []) shipmain.Rows [0] ["ZBDocumentFile"]) .GetUpperBound (0)

FileStream fs = new FileStream (HttpContext.Current.Server.MapPath (shipmain.Rows [0] ["ZBDocument"] .ToString ()), FileMode.OpenOrCreate, FileAccess.Write); / / files are formed from the data in the database

Fs.Write ((byte []) shipmain.Rows [0] ["ZBDocumentFile"], 0, arraySize)

Fs.Close ()

}

Thank you for reading! This is the end of the article on "how to save files in a binary stream to the database". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

Tags: File data database binary code path lieutenant general content array more format article good practical next parameter just article directory read Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Linux OPPO Reno Microsoft Redmi