upload a song into database...
-
how to upload a song and retrieve the song from database.. please give any idea about on this....
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
-
how to upload a song and retrieve the song from database.. please give any idea about on this....