SQL Server database code
& Lt; % @ Page Language = & quot; C # & quot; EnableViewState = & quot; true & quot; % & gt; & lt; % @ Import Namespace = & quot; system. data. sqlClient & quot; % & gt; & lt ;! DOCTYPE html PUBLIC & quot;-// W3C // dtd xhtml 1.0 Transitional // EN & quot; http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script Runat = & quot; server & quot; & gt; string strCnn = & quot; Persist Security Info = False; User ID = sa; Password =; Initial Catalog = Book; server = (local); & quot; protected void button#click (object sender, EventArgs e) {System. IO. stream fileDataStream = FileUpload1.PostedFile. inputStream; if (fileDataStream. length & lt; 1) {Msg. text = & quot; select a file. & Quot; return ;}// get the file size int fileLength = FileUpload1.PostedFile. contentLength; // create an array byte [] fileData = new byte [fileLength]; // fill in the file stream to the array fileDataStream. read (fileData, 0, fileLength); // obtain the file type string fileType = FileUpload1.PostedFile. contentType; // construct a database connection, SQL statement, and create the SqlConnection myConnection = new SqlConnection (strCnn) parameter; SqlCommand command = new SqlCommand (& quot; INSERT INTO UserPhoto (UserName, ContentType, P Hoto) & quot; + & quot; VALUES (@ UserName, @ ContentType, @ Photo) & quot;, myConnection); command. parameters. addWithValue (& quot; @ UserName & quot;, TextBox1.Text); command. parameters. addWithValue (& quot; @ ContentType & quot;, fileType); command. parameters. addWithValue (& quot; @ Photo & quot;, fileData); // open the connection and run the query myConnection. open (); command. executeNonQuery (); myConnection. close (); Response. redirect (Request. rawUrl);} protecte D void Page_Load (object sender, EventArgs e) {if (! Page. isPostBack) {BindGrid () ;}} private void BindGrid () {SqlConnection myConnection = new SqlConnection (strCnn); SqlCommand myCommand = new SqlCommand (& quot; SELECT * FROM UserPhoto Order By id DESC & quot;, myConnection); try {myConnection. open (); GridView1.DataSource = myCommand. executeReader (System. data. commandBehavior. closeConnection); GridView1.DataBind ();} catch (Exception SQLexc) {Response. write (& quot; extract An error occurred while retrieving data: & quot; + SQLexc. ToString () ;}} protected string FormatURL (object strArgument) {return & quot; ReadImage. aspx? Id = & quot; + strArgument. ToString () ;}& lt;/script & gt; & lt; html xmlns = & quot; http://www.w3.org/1999/xhtml"><pead Id = & quot; Head1 & quot; runat = & quot; server & quot; & gt; & lt; title & gt; upload files to the database & lt;/title & gt; & lt;/head & gt; & lt; body & gt; & lt; form id = & quot; MengXianhui & quot; runat = & quot; server & quot; & gt; & lt; asp: GridView ID = & quot; GridView1 & quot; runat = & quot; server & quot; AutoGenerateColumns = & quot; false & quot; & gt; & lt; columns & gt; & lt; asp: TemplateField & gt; & lt; ItemTemplate & gt; & lt; % # Eval (& quot; UserName & quot;) % & gt; & lt;/ItemTemplate & gt; & lt;/asp: TemplateField & gt; & lt; ItemTemplate & gt; & lt; img src = & quot; & lt; % # FormatURL (Eval (& quot; id & quot;) % & gt; & quot;/& gt; & lt; /ItemTemplate & gt; & lt;/asp: TemplateField & gt; & lt;/Columns & gt; & lt;/asp: GridView & gt; & lt; br/& gt; & lt; br/& gt; Name: & lt; asp: TextBox ID = & quot; TextBox1 & quot; runat = & quot; server & quot; & gt; & lt;/asp: TextBox & gt; & lt; br/& gt; Photo: & lt; asp: FileUpload ID = & quot; FileUpload1 & quot; runat = & quot; server & quot;/& gt; & lt; asp: Button ID = & quot; btnUpload & quot; runat = & quot; server & quot; text = & quot; upload & quot; OnClick = & quot; button#click & quot; & gt; & lt;/asp: Button & gt; & lt; p & gt; & lt; asp: Label ID = & quot; Msg & quot; runat = & quot; server & quot; ForeColor = & quot; Red & quot; & gt; & lt; /asp: Label & gt; & lt;/p & gt; & lt;/form & gt; & lt;/body & gt; & lt;/html & gt;