2015-03-13 81 views
-2

請幫助我在SQL Server管理工作室連接上保存此音頻文件。使用VB.net 2010在SQL Server Management Studio上保存音頻文件

我在vb.net 2010上有一個錄音機,我想將我錄製的音頻保存在SQL中,並同時將它顯示在DataGridview(vb.net 2010)上,並將它保存在我的數據庫中,即SQL。

請幫助我們,在數據庫上這個節省的東西有什麼合適的代碼。請不要使用C#和C++作爲指南或源代碼。只有VB.net

+0

我只能建議以小步驟拆分整個問題。嘗試解決一個寫作問題,然後繼續下一步。如果你被卡住了,那麼就來這裏解決一個實際問題。你期望在這裏得到完整的解決方案嗎? – Steve 2015-03-13 09:39:31

+0

是否可能或不可能獲得完整的解決方案?無論如何,謝謝你的好意:)請儘量幫助我 – 2015-03-16 00:43:44

回答

1

在您的D:驅動器中創建一個Access數據庫並將其命名SoundDB.accdb 在數據庫中創建一個表並將其命名爲帶有3個字段的tblSounds sndid text(2),sndname text(20),sndbinary OLEOBJECT)

在你的項目中創建一個新的形式

添加一個文本框和3個按鈕。

在文本框中鍵入一個ID號(2位)

按鈕1將開始錄製

BUTTON2將停止錄像和聲音保存到數據庫

BUTTON3將從DB聲音取爲您在文本框中指定的ID並播放它


將這些行添加到代碼的開頭。

進口System.IO

進口System.Text

進口System.Data.OleDb


此聲明添加到您的代碼

聲明函數mciSendString庫「WINMM。 dll「Alias」mciSendStringA「(_ ByVal lpstrCommand As String,_ BYVAL lpstrReturnString作爲StringBuilder的,_ BYVAL uReturnLength作爲整數,_ BYVAL hwndCallback作爲IntPtr的)作爲整數


下面的子例程粘貼到你的代碼

Private Sub SaveSndtoDB(ByVal SndID As String, ByVal sndName As String) 

    Dim Connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\SoundDB.accdb") 

    Dim SndSourceStream As Stream = New FileStream("D:\FileName.wav", FileMode.Open, FileAccess.Read) 

    Dim BinarySndReader As New BinaryReader(SndSourceStream) 
    Dim bytes As Byte() = BinarySndReader.ReadBytes(SndSourceStream.Length) 

    Connection.Open() 

    'insert the file into database 
    Dim cmd As New OleDb.OleDbCommand("INSERT INTO tblSounds (sndid,sndname,sndbinary) VALUES ('" + SndID + "','" + sndName + "',@SndBinary)", Connection) 

    cmd.Parameters.AddWithValue("@SoundBinary", bytes) 
    Try 
     cmd.ExecuteNonQuery() 
     MsgBox("File Uploaded Successfully") 

    Catch ex As Exception 
     MsgBox("There was a problem, file upload was not successful. " + ex.Message) 
    End Try 

    SndSourceStream.Flush() 
    SndSourceStream.Close() 

    FileClose() 
    Reset() 

    Application.DoEvents() 

End Sub 

Private Sub PlaySndfromDB(ByVal sndID As String) 

    FileClose() 
    Reset() 

    Dim Connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\SoundDB.accdb") 

    Dim SndSourceStream As Stream = New FileStream("D:\FileName.wav", FileMode.Create, FileAccess.Write) 

    Try 
     Connection.Open() 

     Dim command As New OleDbCommand("SELECT sndbinary FROM tblsounds WHERE sndID = @ID", Connection) 


     command.Parameters.AddWithValue("@ID", sndID) 

     Dim soundData As Byte() = DirectCast(command.ExecuteScalar(), Byte()) 

     Connection.Close() 
     command.Dispose() 

     SndSourceStream.Write(soundData, 0, soundData.Length) 

     SndSourceStream.Flush() 
     SndSourceStream.Close() 

     My.Computer.Audio.Play("d:\FileName.wav", AudioPlayMode.Background) 

    Catch ex As Exception 
     MsgBox(ex.ToString()) 
    End Try 


End Sub 




Private Sub StartRecording() 

    mciSendString("open new type waveaudio alias mywav", Nothing, 0, 0) 
    mciSendString("record mywav", Nothing, 0, 0) 

End Sub 

Private Sub SaveRecording() 

    mciSendString("stop mywav", Nothing, 0, 0) 
    mciSendString("save mywav d:\FileName.wav", Nothing, 0, 0) 
    mciSendString("close mywav", Nothing, 0, 0) 

End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    StartRecording() 
End Sub 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 

    SaveRecording() 
    Application.DoEvents() 
    SaveSndtoDB(TextBox1.Text, "Snd" + TextBox1.Text) 

End Sub 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 

    PlaySndfromDB(TextBox1.Text) 

End Sub 

======= ================================================== =======================

希望這可以幫助,隨時讓我知道如果你需要進一步的幫助。 我還沒有用很長的聲音文件測試過這段代碼,它可以和片段一起使用。

相關問題