ASP. NET accessing SQL Server database images can be said to be a headache, and a system I want to do is just to show the user's photos, 555, different people's photos are certainly different! Ah, no one guides you, but you just need to explore it on your own. Hey, after a day of hard work, I finally solved this problem.
I searched for some related content on the Internet. The method is roughly the same, that is, to access the image in binary format. I can see that many codes are too large. Is there a simpler method, I asked my cousin, who specializes in software development. She said she could put the photo in a folder and call it in the database. She didn't say the specific method, but there is a good idea.
I did two simple page experiments:
First, the upfile. ASPX page is used to upload images
The Code is as follows:
<% @ Page Language = "VB" autoeventwireup = "false" codefile = "upfile. aspx. VB" inherits = "upfile" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://hi.baidu.com/highadmin">
<HTML xmlns = "http://hi.baidu.com/highadmin">
<Head runat = "server">
<Title> upfile </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: fileupload id = "fileupload1" runat = "server"/> <br/>
<Br/>
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox> <br/>
<Br/>
<Asp: textbox id = "textbox2" runat = "server"> </ASP: textbox> <br/>
<Br/>
<Asp: sqldatasource id = "sqldatasource1" runat = "server" conflictdetection = "compareallvalues"
Connectionstring = "<% $ connectionstrings: msisconnectionstring %>" insertcommand = "insert into [gallery] ([uploadbymembername], [Notes], [pictureurl]) values (@ uploadbymembername, @ notes, @ pictureurl )"
Oldvaluesparameterformatstring = "original _ {0}" selectcommand = "select * from [gallery]"
<Insertparameters>
<Asp: controlparameter name = "uploadbymembername" controlid = "textbox1" propertyname = "text" type = "string"/>
<Asp: controlparameter name = "Notes" controlid = "textbox2" propertyname = "text" type = "string"/>
<Asp: controlparameter name = "pictureurl" controlid = "fileupload1" propertyname = "FILENAME" type = "string"/>
</Insertparameters>
</ASP: sqldatasource>
<Br/>
<Asp: button id = "button1" runat = "server" text = "button"/> </div>
</Form>
</Body>
</Html>
The upfile. aspx. VB Code is as follows:
Partial class upfile
Inherits system. Web. UI. Page
Protected sub button#click (byval sender as object, byval e as system. eventargs) handles button1.click
If fileupload1.hasfile then
Try
Fileupload1.saveas ("D: \ student information management system \ website \ images \" & fileupload1.filename)
Catch ex as exception
End try
Sqldatasource1.insert ()
End if
End sub
End Class
The data display page is very simple, and a datalist control is used.
The datalist. aspx code is as follows:
<% @ Page Language = "VB" autoeventwireup = "false" codefile = "datalist. aspx. VB" inherits = "datalist" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://hi.baidu.com/highadmin">
<HTML xmlns = "http://hi.baidu.com/highadmin">
<Head runat = "server">
<Title> datalist </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: datalist id = "datalist1" runat = "server" datakeyfield = "pictureid" performanceid = "sqlperformance1">
<Itemtemplate>
Pictureid:
<Asp: Label id = "pictureidlabel" runat = "server" text = '<% # eval ("pictureid") %>'> </ASP: Label> <br/>
Uploadbymembername:
<Asp: Label id = "uploadbymembernamelabel" runat = "server" text = '<% # eval ("uploadbymembername") %>'> </ASP: Label> <br/>
Notes:
<Asp: Label id = "noteslabel" runat = "server" text = '<% # eval ("Notes") %>'> </ASP: Label> <br/>
<Asp: Image id = "image1" runat = "server" imageurl = '<% # eval ("pictureurl ","~ /Images/{0} ") %> '/> <br/>
<Br/>
</Itemtemplate>
</ASP: datalist> <asp: sqldatasource id = "sqlcece1" runat = "server" connectionstring = "<% $ connectionstrings: msisconnectionstring %>"
Selectcommand = "select * from [gallery]"> </ASP: sqldatasource>
</Div>
</Form>
</Body>
</Html>
Principle: upload an image to a specified folder, and write the file name (non-path, but file name) of the uploaded image into the database of the relevant user, when the image is displayed, add an imagin control to datalist to bind the data!
Save:
(Query1.fieldbyname ('img ') as tblobfield). loadfromfile ('e: \ temp.bmp ');
Retrieve:
(Query1.fieldbyname ('img ') as tblobfield). savetofile ('e: \ temp.bmp ');
Try to save a large graph.