将Video文件保存到数据库,然后再从数据库显示到axWindowsMediaPlayer1上
来源:互联网 发布:淘宝(中国)有限公司 编辑:程序博客网 时间:2024/06/02 09:39
将Video文件保存到数据库,然后再从数据库显示到axWindowsMediaPlayer1上
具体思路如下:
1.将Video文件转化为二进制数据,将二进制数据,文件名保存到数据库对应的表中
2.为了将Video显示在Winform的axWindowsMediaPlayer1中,可以从数据库中读取出数据,根据保存的Video文件的二进制数据,将二进制数据转化为临时文件保存在临时目录中,然后把临时文件的路径绑定到axWindowsMediaPlayer1显示播放。
具体的代码如下:
private void SaveToBase_Click(object sender, EventArgs e) { string filePath = GetFile(); string[] list = new string[] { ".mp3", ".avi", ".mp4", ".wmv" }; if (!list.Contains(Path.GetExtension(filePath))) { MessageBox.Show("Please select proper file."); } else { //读取filepath的文件 FileStream fs = new FileStream(filePath,FileMode.Open,FileAccess.Read); byte[] videoByteArr = new byte[fs.Length]; //将二进制内容存储在videoByteArr fs.Read(videoByteArr,0,Convert.ToInt32(fs.Length)); fs.Close(); //将videoByteArr保存到数据库 if (SaveToDataBase(Path.GetFileName(filePath), videoByteArr)) { cmbPlayList.Items.Add(Path.GetFileName(filePath)); MessageBox.Show("Save OK!!!"); } else { MessageBox.Show("Error!!!"); } } } private void OpenFromBase_Click(object sender, EventArgs e) { string fileName = cmbPlayList.SelectedItem.ToString(); string filePath = Path.GetTempPath() + "\\" + fileName; if (File.Exists(filePath)) { File.Delete(filePath); } SqlConnection connection = new SqlConnection(ConnectionString); string qurey = "select content from TestTable where filename = " + "'" + fileName + "'"; SqlDataAdapter adapter = new SqlDataAdapter(qurey, connection); DataSet ds = new DataSet(); adapter.Fill(ds, "MyPlay"); if (ds.Tables[0].Rows.Count == 0) { MessageBox.Show("No data found"); } else { FileStream fs = new FileStream(filePath,FileMode.Create); byte[] videoByteArr = (byte[])ds.Tables[0].Rows[0]["content"]; fs.Write(videoByteArr,0,videoByteArr.Length); fs.Close(); fs = null; } axWindowsMediaPlayer1.URL = filePath; axWindowsMediaPlayer1.settings.autoStart = true; } public bool SaveToDataBase(string fileName, byte[] data) { try { var ds = new DataSet(); SqlCommand cmd = new SqlCommand("insert into TestTable 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 e) { throw; } return false; }
其中的内容参照了下面的文章
# Playing Audio and Video File in Windows Form Application
http://www.c-sharpcorner.com/uploadfile/26b237/playing-audio-and-video-file-in-windows-form-application/
0 0
- 将Video文件保存到数据库,然后再从数据库显示到axWindowsMediaPlayer1上
- 如何将pictureBox里的图片保存到数据库,然后从数据读取显示
- 将excel中数据显示在页面上,然后再导入到SqlServer数据库中
- 页面上的复选框,然后保存到数据库,再从数据库取出来,看那个复选框被选中
- VB6保存文件到数据库,从数据库保存为文件
- 将文件保存到数据库中
- 将文件保存到数据库中(stream)
- C#将文件保存到数据库中
- c# 将文件保存到数据库
- 将文件保存到数据库中
- 将文件保存到数据库中
- 将文件保存到数据库中
- 将图片保存到数据库表中及从数据库表中读取图片并显示
- C#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- c#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- 关于大任务滚动条处理思路
- Flash 培训教程网址
- (hk2 转折)
- Android支付接入(三):电信爱游戏支付
- Evaluate Reverse Polish Notation
- 将Video文件保存到数据库,然后再从数据库显示到axWindowsMediaPlayer1上
- sql server 2008使用教程
- 词法分析(lexical analysis)
- LeetCode | Best Time to Buy and Sell Stock III
- (hk1 2014 机会)
- list中 append和extend的区别
- 有1到10w这10w个数,去除2个并打乱次序,如何找出那两个数。(不准用位图!!)
- 串口WIF简单I调试
- Android View绘制--自定义控件原理