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. General Programming
  3. C#
  4. upload a song into database...

upload a song into database...

Scheduled Pinned Locked Moved C#
databasetutorial
3 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.
  • S Offline
    S Offline
    sri apple
    wrote on last edited by
    #1

    how to upload a song and retrieve the song from database.. please give any idea about on this....

    L A 2 Replies Last reply
    0
    • S sri apple

      how to upload a song and retrieve the song from database.. please give any idea about on this....

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      1-Create database with table : tbSongs guid | songname | songfile ---------------------------- guid uniuqeidentfieir songname varchar(255) songfile image 2-Convert your song to byte array

      public static byte [] File2ByteArray (string szFileName)
      {
      Stream st = new FileStream (szFileName,FileMode.Open);
      byte[] bit = new byte[st.Length];
      st.Read(bit, 0 , (int)st.Length);
      st.Close();
      return bit;
      }

      3-Store it in tbSongs

      #region Fields
      
      private Guid \_guid;
      public Guid guid
      {
          get
          {
              return \_guid;
          }
          set
          {
              \_guid = value;
          }
      }
      private string \_songname;
      public string songname
      {
          get
          {
              return \_songname;
          }
          set
          {
              \_songname = value;
          }
      }
      private Byte\[\] \_song;
      public Byte\[\] song
      {
          get
          {
              return \_song;
          }
          set
          {
              \_song = value;
          }
      }
      
      #endregion
      
      #region Database
      public void Insert()
      {
          DBConnect.DBCommand.CommandText = "INSERT INTO tbSongs VALUES (?, ?, ?)";
      
          DBConnect.DBCommand.Parameters.AddWithValue("@guid", guid);
          DBConnect.DBCommand.Parameters.AddWithValue("@songname", songname);
          DBConnect.DBCommand.Parameters.AddWithValue("@image", song);
      
          DBConnect.DBCommand.ExecuteNonQuery();
          DBConnect.DBCommand.Parameters.Clear();
      }
      

      to retrive the song from database : Find you song with guid or name and cast datareader to bytes array.

      public static byte\[\] GetFilebytes(guid guid)
      {
          DBCommand = new DBCommand("SELECT songfile FROM tbsongs WHERE guid = @guid", DataBase.DBConnection);
          DBCommand.Parameters.AddWithValue("@guid", guid);
          byte\[\] filebytes = (byte\[\])DBCommand.ExecuteScalar();
          DataBase.DBCommand.Parameters.Clear();
          return filebytes;
      }
      

      4-Use File Stream or Memory Stream to Save your Song

      byte[] filebites = GetFilebytes(songguid)
      FileStream fs = new FileStream("c:\\mysong.mp3", FileMode.CreateNew, FileAccess.Write);
      BinaryWriter bw = new BinaryWriter(fs);
      bw.Write(filebytes);
      bw.Close();
      fs.Close();

      Hope this help

      1 Reply Last reply
      0
      • S sri apple

        how to upload a song and retrieve the song from database.. please give any idea about on this....

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        You can use a BLOB (binary large object) to store song data into the database. This[^] might help.

        WP7.5 Apps - XKCD | Calvin | SMBC | Sound Meter | Speed Dial

        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