G
Guest
I have an application that allows users to view images and hear sound files.
The images and soundclips are stores in a SQL database in image fields.
Originally, I transferred the data from an Access database using the Upsize
wizard. I have been able to retrieve the data from the SQL database using
memorystreams. To do so, I have been using offests to bypass a header that is
present: for the images, I use offest 78 and for the sounds, it's 86.
All has been well and good but now I need to load images and sound clips
directly using my app, not Access. I have tried uploading the files using no
offset and/or the offsets I have previously needed to use, all to no avail.
For instance, after I store a .wav file to the database, I receive a message
"The wave header is corrupt".
Now, I see all kinds of examples to store and retrieve blob data on the
internet. None of them deal with headers.
Can anyone clear up this mystery? I am using VS 2005 Express C# edition and
SQL Express 2005.
Code examples:
public void PlaySound()
{
if (ds.Tables["Cards"] != null)
{
if (isSound)
{
sqlConn.Open();
SqlDataAdapter sql = new SqlDataAdapter(
"SELECT Sound FROM Sounds WHERE Sounds.CardID=" +
ds.Tables["Cards"].Rows[curMgr.Position]["CardID"].ToString(),
sqlConn);
DataTable dt = new DataTable();
sql.Fill(dt);
sqlConn.Close();
DataRow dr = dt.Rows[0];
byte[] result = (byte[])dr["Sound"];
try
{
// Retrieve the sound file using the header offset
(86)
MemoryStream ms = new MemoryStream(result,
SNDHDROFFSET, result.Length - SNDHDROFFSET);
SoundPlayer sp = new SoundPlayer(ms);
sp.Play();
}
catch (Exception e)
{
MessageBox.Show("Sound header error! The sound clip
is not properly formatted in the SQL database. "+ e.Message);
}
}
}
}
}
public void AddSound(string soundFilename)
{
SqlCommand cmd = new SqlCommand();
FileStream stream = new FileStream(soundFilename, FileMode.Open,
FileAccess.Read);
byte[] snd = new byte[stream.Length];
stream.Read(snd, 0, (int)stream.Length);
stream.Close();
sqlConn.Open();
if (!isSound)
cmd = new SqlCommand("InsertSound", sqlConn);
else
cmd = new SqlCommand("UpdateSound", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CardID", SqlDbType.Int).Value =
ds.Tables["Cards"].Rows[curMgr.Position]["CardID"];
cmd.Parameters.Add("@Sound", SqlDbType.Image).Value = snd;
cmd.ExecuteNonQuery();
sqlConn.Close();
isSound = true;
}
ALTER PROCEDURE dbo.InsertSound
(
@CardID int,
@Sound image
)
AS
INSERT INTO Sounds (CardID, Sound)
VALUES (@CardID, @Sound)
RETURN
The images and soundclips are stores in a SQL database in image fields.
Originally, I transferred the data from an Access database using the Upsize
wizard. I have been able to retrieve the data from the SQL database using
memorystreams. To do so, I have been using offests to bypass a header that is
present: for the images, I use offest 78 and for the sounds, it's 86.
All has been well and good but now I need to load images and sound clips
directly using my app, not Access. I have tried uploading the files using no
offset and/or the offsets I have previously needed to use, all to no avail.
For instance, after I store a .wav file to the database, I receive a message
"The wave header is corrupt".
Now, I see all kinds of examples to store and retrieve blob data on the
internet. None of them deal with headers.
Can anyone clear up this mystery? I am using VS 2005 Express C# edition and
SQL Express 2005.
Code examples:
public void PlaySound()
{
if (ds.Tables["Cards"] != null)
{
if (isSound)
{
sqlConn.Open();
SqlDataAdapter sql = new SqlDataAdapter(
"SELECT Sound FROM Sounds WHERE Sounds.CardID=" +
ds.Tables["Cards"].Rows[curMgr.Position]["CardID"].ToString(),
sqlConn);
DataTable dt = new DataTable();
sql.Fill(dt);
sqlConn.Close();
DataRow dr = dt.Rows[0];
byte[] result = (byte[])dr["Sound"];
try
{
// Retrieve the sound file using the header offset
(86)
MemoryStream ms = new MemoryStream(result,
SNDHDROFFSET, result.Length - SNDHDROFFSET);
SoundPlayer sp = new SoundPlayer(ms);
sp.Play();
}
catch (Exception e)
{
MessageBox.Show("Sound header error! The sound clip
is not properly formatted in the SQL database. "+ e.Message);
}
}
}
}
}
public void AddSound(string soundFilename)
{
SqlCommand cmd = new SqlCommand();
FileStream stream = new FileStream(soundFilename, FileMode.Open,
FileAccess.Read);
byte[] snd = new byte[stream.Length];
stream.Read(snd, 0, (int)stream.Length);
stream.Close();
sqlConn.Open();
if (!isSound)
cmd = new SqlCommand("InsertSound", sqlConn);
else
cmd = new SqlCommand("UpdateSound", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CardID", SqlDbType.Int).Value =
ds.Tables["Cards"].Rows[curMgr.Position]["CardID"];
cmd.Parameters.Add("@Sound", SqlDbType.Image).Value = snd;
cmd.ExecuteNonQuery();
sqlConn.Close();
isSound = true;
}
ALTER PROCEDURE dbo.InsertSound
(
@CardID int,
@Sound image
)
AS
INSERT INTO Sounds (CardID, Sound)
VALUES (@CardID, @Sound)
RETURN