In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.