Wednesday, December 3, 2014

How to video upload to database and watch video in the form

Hi Karthik,


Based on your description, I think you could save the byte data of the video to database. When you want to watch it, you could get the byte data from database.


There are some key codes below:



//Here is the code to save the file in the database:
private bool SaveToDataBase(string fileName, byte[] data)
{
try
{
var ds = new DataSet();
SqlCommand cmd = new SqlCommand("insert into MyPlay values('" + Guid.NewGuid() + "','" + fileName + "',@content)");
SqlParameter param = cmd.Parameters.Add("@content", SqlDbType.VarBinary);
param.Value = data;
cmd.Connection = new SqlConnection(ConnectionString);
cmd.CommandTimeout = 0;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception)
{
throw;
}
return false;
}
//get the file from database
private string GetFromDataBase(string fileName)
{
try
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
String Query1 = "SELECT FileData FROM [Test].[dbo].[MyPlay] where FileName = '" + fileName + "'";
SqlDataAdapter adapter = new SqlDataAdapter(Query1, ConnectionString);
DataSet Ds = new DataSet();
adapter.Fill(Ds, "MyPlay");
if (Ds.Tables[0].Rows.Count == 0)
{
MessageBox.Show("No data Found");
return string.Empty;
}
return ConvertByteDataToFile(fileName, GetUnCompressedData((byte[])Ds.Tables[0].Rows[0]["FileData"]));
}
catch (Exception)
{
throw;
}
return string.Empty;
}
//Show the video
private void btnPlay_Click(object sender, EventArgs e)
{
if (cmbPlayList.SelectedItem == null)
{
MessageBox.Show("Please select file to play."); return;
}
axWindowsMediaPlayer1.URL = GetFromDataBase(cmbPlayList.SelectedItem.ToString());
axWindowsMediaPlayer1.settings.autoStart = true;
}

The link below shows the details.


# Playing Audio and Video File in Windows Form Application

http://ift.tt/1eDTbLu


If you have any further questions, please feel free to post in this forum.


Best Regards,


LEO


No comments:

Post a Comment