---------------------- ASP. NET + Android + I/O Development S,. Net training, hope to communicate with you! ----------------------
If you do not have enough to write a blog for the first time, please forgive me!
The simple storage of big data in databases is that two methods can be implemented. One is to upload the data to the database, and the other is to extract the data from the database.
For ease of understanding, I will give a simple windows form application example to implement the upload and download function (movie upload and download)
This is the specific design of the interface, so I will not be arrogant.
Let's take a look at how the browser button is implemented.
Private void btnbrowser_click (Object sender, eventargs e) {openfiledialog opendlg = new openfiledialog (); opendlg. filter = "video file | *. WMV ;*. MP4 ;*. avi "; if (opendlg. showdialog ()! = Dialogresult. OK) return; this. lblfilename. Text = opendlg. filename; // This. mediaplayer. url = This. lblfilename. Text ;}
This is the video file obtained from your various storage media and then the complete path of the file is handed over to our text box to save it. It is very useful to keep the following for uploading.
Next we should focus on the button for uploading.
Private void btnupload_click (Object sender, eventargs e) {If (this. lblfilename. TEXT = "") return; If (this. uploadfiletodb (this. lblfilename. text) {MessageBox. show ("complete upload ");}}
You can see that no upload button is available. There is no code for uploading. You can call only the uploadfiletodb () method ();
Private bool uploadfiletodb (string filename) {string cnnstring = "Server = .; database = largedatadb; uid = sa; Pwd = 123456 "; string plain text =" usp_videos_insert "; // This is a stored procedure. If you want to write an SQL statement, you can use the following commandtype. change storedprocedure to text to sqlconnection CNN = new sqlconnection (cnnstring); CNN. open (); sqlcommand cmd = new sqlcommand (); cmd. connection = CNN; cmd. commandtext = plain text; cmd. commandtype = commandtype. storedprocedure; cmd. parameters. add ("@ FILENAME", sqldbtype. varchar ). value = filename. substring (filename. lastindexof (@ "\") + 1); // obtain the file name filestream stream = new filestream (filename, filemode. open); // create a file stream byte [] buffer = new byte [stream. length]; stream. read (buffer, 0, buffer. length); stream. close (); cmd. parameters. add ("@ data", sqldbtype. image ). value = buffer; // send the obtained binary data to the data parameter int rowcount = CMD in the stored procedure. executenonquery (); CNN. close (); If (rowcount> 0) {return true;} else {return false ;}}
If the file is too large, it is best to add multithreading technology to avoid deadlock in the main thread.
Next let's take a look at the download feature.
Private void btndownload_click (Object sender, eventargs e) {int id = int.parse(this.txt ID. Text); string filename = This. downfilefromdb (ID); If (filename! = String. Empty) {This. mediaplayer. url = filename; // get }}
A method is also called to download dowmfilefromdb ();
Private string downfilefromdb (int id) {string cnnstring = "Server = .; database = largedatadb; uid = sa; Pwd = Master "; string character text =" select filename, data from videos where id = @ ID "; // here, the parameter is used to obtain the object to be downloaded through ID sqlconnection CNN = new sqlconnection (cnnstring); CNN. open (); sqlcommand cmd = new sqlcommand (); cmd. connection = CNN; cmd. commandtext = plain text; cmd. commandtype = commandtype. text; cmd. parameters. add ("@ ID", sqldbtype. INT ). value = ID; sqldatareader reader = cmd. executereader (); string filename = string. empty; byte [] buffer = NULL; while (reader. read () {filename = (string) Reader ["FILENAME"]; filename = "D: \" + filename; buffer = (byte []) reader ["data"]; filestream stream = new filestream (filename, filemode. create); // write the binary data obtained from the database to the stream on the hard disk. write (buffer, 0, buffer. length); stream. close () ;}return filename ;}
Now, upload and download are here. Let's take a look at the results. This article is only intended to implement the upload and download functions. The code is a bit simple and does not use a hierarchical structure. I hope you will forgive me! This not only stores the image type in the video database, but also stores all binary files. I don't know how much better this article is for the design of the product. Thank you for your support!
-------------------- ASP. NET + Android + iOS development,. Net training, and hope to communicate with you! ----------------------
See http://edu.csdn.net for details