Many of my friends asked me how to write the images you selected on the webpage to the database field. We wrote a webpage example to demonstrate how to implement it. The procedure for this web page example is as follows:
1. as shown in table 1, click "create data table before Database" to create a data table named "exercise Author" in the "North Wind trade" database, it is used to store data records including photos.
Chart 1
2. See table 2. Click Browse to select a photo.
Chart 2
3. See table 3. In the select file dialog box, select the desired photo and press the OPEN button.
Chart 3
4. As shown in table 4, Click Upload and write to database.
Chart 4
5. repeat the preceding steps until you have added the required data records, as shown in table 5, click the hyperlink "View data table photo data" to view the data uploaded and written to the database fields.
Chart 5
After learning how to operate the web page sample, we will examine its development skills. I think the most important skill in this example is how to add data records, including photos, to data tables.ProgramCodeThe click event processing routine written in the upload and write to database button is listed as follows:
Protected sub btnupload_click (byval sender as object ,_
Byval e as system. eventargs) handles btnupload. Click
'Use the sqlconnectionstringbuilder object to construct the connection string.
Dim connectstringbuilder as new sqlconnectionstringbuilder ()
Connectstringbuilder. datasource = "(local) \ sqlexpress"
Connectstringbuilder. initialcatalog = "beifeng trade"
Connectstringbuilder. integratedsecurity = true
Try
'Establish a connection.
Using con as new sqlconnection (connectstringbuilder. connectionstring)
Dim data () as byte = _
My. Computer. filesystem. readallbytes (Me. file1.value. Trim ())
/*
Dim filelen as integer
'Get the length of the uploaded photo file.
Filelen = fileupload1.postedfile. contentlength
'creates a byte array to hold the file content.
dim input (filelen) as byte
input = fileupload1.filebytes
*/
dim updatecmd as sqlcommand = _
New sqlcommand (_
" insert into exercise author (Author name, author: Yu Zhao) values (@ authorname, @ photo) ", _
con)
Dim authornameparameter as sqlparameter = _
New sqlparameter ("@ authorname", sqldbtype. nvarchar, 10)
If me.txt authorname. Text. Trim (). Equals ("") then
Authornameparameter. value = "Zhang Limin Research Office"
Else
Authornameparameter. value = me.txt authorname. Text. Trim ()
End if
Updatecmd. Parameters. Add (authornameparameter)
Dim photoparameter as sqlparameter = _
New sqlparameter ("@ photo", sqldbtype. Image)
Photoparameter. value = Data
Updatecmd. Parameters. Add (photoparameter)
Con. open ()
Updatecmd. executenonquery ()
Con. Close ()
Me. lblmessage. Text = "successfully added data records"
End using
Catch ex as exception
Me. lblmessage. Text = "error" & vbcrlf & Ex. tostring
Throw new exception (ex. Message)
End try
End sub
Reference data:
Visual Basic 2005 file I/O and data access tips-ch7: exploring large objects (LOB)
Accesskey
Visual C #2005 secrets of file I/O and data access-ch7: Exploring the secrets of Large Object (LOB) Access