2014-10-27 74 views
1

我將音頻文件成功保存在MySQL數據庫中,但我不知道如何從數據庫中檢索和播放。任何幫助將不勝感激。使用c插入和檢索MySQL中的音頻文件#

這是我插入代碼:

private void button2_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     MySqlConnection connection; 
     string cs = @"server = 127.0.0.1; userid = root; pwd = ; database = atlfmdb;"; 
     connection = new MySqlConnection(cs); 

     if (upload.Text.Length > 0 && 
      vName.Text.Length > 0 && 
      vTel.Text.Length>0 && 
      tbposition.Text.Length>0) 
     { 
      string FileName = upload.Text; 

      byte[] f = File.ReadAllBytes(upload.Text) ; 

      MySqlCommand selectcom = new MySqlCommand("insert into interinterview(intName,intPosition,intTel,audioFile)values('" + vName.Text + "','" + tbposition.Text + "','" + vTel.Text + "','" + f + "')", connection); 
      MySqlDataReader myread; 

      connection.Open(); 
      myread = selectcom.ExecuteReader(); 
      while (myread.Read()) 
      { 
      } 

      connection.Close(); 
      MessageBox.Show("Data saved successfully"); 
      vName.Text = ""; 
      vTel.Text = ""; 
      tbposition.Text = ""; 
      upload.Text = ""; 
     } 
    } 
    catch(Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
     openFileDialog1.Filter = "Audio files | *.mp3"; 

     if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      upload.Text = openFileDialog1.FileName; 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 
} 
} 
+4

我不明白。你將這些值保存到你的數據庫,但你不能從數據庫中讀取它們?你應該總是使用[參數化查詢](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/)。這種字符串連接對於[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)攻擊是開放的。並使用'using'語句處理您的數據庫連接和對象 – 2014-10-27 13:11:24

+0

我可以檢索其餘數據,但不能檢索音頻文件。 – 2014-10-27 14:03:22

+0

我試圖使用媒體播放器播放音頻文件,但它說文件有.datarowview擴展名 – 2014-10-29 13:04:55

回答

0

如果你還沒有制定出解決方案,我剛剛工作了基於該鏈接的答案: How to play audio file located on server in asp.net

在這個鏈接的結尾給了你Dewplayer zip包的位置。下載並將dewplayer-vol.swf文件放入根文件夾中。

您需要將所有MS SQL命令轉換爲MySQL,並將MySQL.DLL放入bin文件夾中,如果不存在的話。就我而言,我存儲的MP3文件作爲varbinay和使用存儲過程是這樣的檢索:在這個環節

private void BindGrid() 
{ 
    string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlServer"].ConnectionString; 

    using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connStr)) 
    { 

     using (MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("getaudio", conn)) 
     { 
      cmd.CommandType = CommandType.StoredProcedure; 
      conn.Open(); 
      using (IDataReader idr = cmd.ExecuteReader()) 
      { 
       GridView1.DataSource = idr; 
       GridView1.DataBind(); 
      } 
      conn.Close();    
     } 
    } 
} 

更多細節: http://aspsnippets.com/Articles/Save-MP3-Audio-Files-to-database-and-display-in-ASPNet-GridView-with-Play-and-Download-option.aspx

你需要創建一個文件說音頻。 ashx並將其放入根文件夾。希望這可以幫到你。

相關問題