<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HW5._Default" %> <script> public class DataAccess { private string _strConn = @"Driver= {MySQLODBC 3.51 Driver};SERVER=localhost;DATABASE=test1;"; private OdbcConnection _objConn; public DataAccess() { this._objConn = new OdbcConnection(this._strConn); } // This function adds the Images to database public string addImage(timestamp id, byte [] data,string extension) { string strSql = "SELECT * FROM File"; DataSet ds = new DataSet("Image"); OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn); OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP); tempAP.Fill(ds,"Table"); try { this._objConn.Open(); DataRow objNewRow = ds.Tables["Table"].NewRow(); objNewRow["Extension"] = extension; objNewRow["Data"] = buffer; objNewRow["ID"] = id; ds.Tables["Table"].Rows.Add(objNewRow); // trying to update the table to add the image tempAP.Update(ds,"Table"); } catch(Exception e){return e.Message;} finally{this._objConn.Close();} return null; }0 // This function to get the image data from the database public byte [] getImage(int imageNumber) { string strSql = "SELECT * FROM File"; DataSet ds = new DataSet("Image"); OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn); OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP); tempAP.Fill(ds,"Table"); try { this._objConn.Open(); byte [] buffer = (byte [])ds.Tables["Table"].Rows[imageNumber]["Data"]; return buffer; } catch{this._objConn.Close();return null;} finally{this._objConn.Close();} } // Get the image count public int getCount() { string strSql = "SELECT COUNT(Data) FROM File"; DataSet ds = new DataSet("Image"); OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn); OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP); tempAP.Fill(ds,"Table"); try> { this._objConn.Open(); int count = (int)ds.Tables["Table"].Rows[0][0]; return count; } catch{this._objConn.Close();return 0;} finally{this._objConn.Close();} } } private void Page_Load(object sender, System.EventArgs e) { //Checking if there are any files avaiable on IIS. if(Request.Files.Count != 0) { HttpPostedFile httpFile = Request.Files[0]; // Checking for extension string extension = this.getFileExtension(httpFile.ContentType); if(extension == null ) { Response.Write("Mime type not Supported"); return; } System.IO.BufferedStream bf = new BufferedStream(httpFile.InputStream); byte[] buffer = new byte; bf.Read(buffer,0,buffer.Length); // Creating the database object DataAccess data = new DataAccess(); // Adding files to the database. data.addImage(buffer,extension); Response.Write("Image Added!"); } } </script> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <asp:Label id="lblImageFile" Text="Image File:" AssociatedControlID="upImage" Runat="server" /> <asp:FileUpload id="upImage" Runat="server" /> <br /><br /> <asp:Button id="btnAdd" Text="Add Image" OnClick="DataAccess.addImage(byte [] buffer,string extension)" Runat="server" /> <hr /> <asp:DataList id="dlstImages" RepeatColumns="3" runat="server"> <ItemTemplate> <asp:Image ID="Image1" ImageUrl='<%# Eval("Name", "~/UploadImages/{0}") %>' style="width:200px" Runat="server" /> <br /> <%# Eval("Name") %> </ItemTemplate> </asp:DataList> </form> </body> </html>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)