Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to store/retrieve a file in a database ...........

How to store/retrieve a file in a database ...........

Scheduled Pinned Locked Moved ASP.NET
databasetutorialcsharpsql-serversysadmin
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jy othi
    wrote on last edited by
    #1

    How to store a file(doc/txt/jpeg) in a database(sql server/access) and how to retrieve it from the database using .net. Can u please send me the step by step approach to achieve this.

    A J 3 Replies Last reply
    0
    • J jy othi

      How to store a file(doc/txt/jpeg) in a database(sql server/access) and how to retrieve it from the database using .net. Can u please send me the step by step approach to achieve this.

      A Offline
      A Offline
      anj1983
      wrote on last edited by
      #2

      save the url of the files in database and design a page that reads the url. It's easy....... if you wants to store the files in the databae that's going to be tricky but not impossible....... If you want more help plz do say so. I have done this. leo

      J 1 Reply Last reply
      0
      • A anj1983

        save the url of the files in database and design a page that reads the url. It's easy....... if you wants to store the files in the databae that's going to be tricky but not impossible....... If you want more help plz do say so. I have done this. leo

        J Offline
        J Offline
        jy othi
        wrote on last edited by
        #3

        thanks for the info.Please confirm me the following. -- I need to store the path(virtual) path of the file(?) -- i need to access the path of the file from the databaseand do the file operations like any other file(?) if it is not so please explain me how to do that and the classes involved in it. thanks, jyothi

        1 Reply Last reply
        0
        • J jy othi

          How to store a file(doc/txt/jpeg) in a database(sql server/access) and how to retrieve it from the database using .net. Can u please send me the step by step approach to achieve this.

          J Offline
          J Offline
          jithen_dt
          wrote on last edited by
          #4

          The following ASPX file reads and displays an image from the Pubs database that comes with Microsoft SQL Server. <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Imaging" %> <%@ Import Namespace="System.IO" %> void Page_Load(object sender, System.EventArgs e) { MemoryStream stream = new MemoryStream (); SqlConnection connection = new SqlConnection ("server=localhost;database=pubs;uid=sa;pwd="); try { connection.Open (); SqlCommand command = new SqlCommand ("select logo from pub_info where pub_id='0736'", connection); byte[] image = (byte[]) command.ExecuteScalar (); stream.Write (image, 0, image.Length); Bitmap bitmap = new Bitmap (stream); Response.ContentType = "image/gif"; bitmap.Save (Response.OutputStream, ImageFormat.Gif); } finally { connection.Close (); stream.Close (); } } ENJOY MAADE

          1 Reply Last reply
          0
          • J jy othi

            How to store a file(doc/txt/jpeg) in a database(sql server/access) and how to retrieve it from the database using .net. Can u please send me the step by step approach to achieve this.

            J Offline
            J Offline
            jithen_dt
            wrote on last edited by
            #5

            /* adds image name specified as parameter in Image table (Database GDP) Stru: Images ImageID int IDENTITY FullName varchar(50) ImageData image */ using System; using System.IO; using System.Data.SqlClient; class StoreImage { public void AddImageToDatabase(String strImageName) { SqlConnection cnObj = new SqlConnection("Data Source=tilsrv-dev1;Initial Catalog=GDP;User ID=sa;password=;"); FileStream stream = new FileStream(strImageName, FileMode.Open); byte[] bytBlob = new byte[stream.Length]; stream.Read(bytBlob, 0, (int)stream.Length); stream.Close(); try { cnObj.Open(); SqlCommand cmdInsertImage = new SqlCommand("INSERT INTO Images(FullName, ImageData) VALUES('" + strImageName + "', @Img)", cnObj); cmdInsertImage.Parameters.Add("@Img", bytBlob); cmdInsertImage.ExecuteNonQuery(); } catch(SqlException e) { Console.WriteLine(e.Message); } finally { cnObj.Close(); } } public static void Main(String[] args) { StoreImage obj; if(args.Length == 0) Console.WriteLine("USAGE: StoreImage "); else { obj = new StoreImage(); obj.AddImageToDatabase(args[0]); } } } ENJOY MAADE.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups