开发者

Save audio and video file in SQL Server database

开发者 https://www.devze.com 2023-04-03 12:40 出处:网络
How can I save 开发者_JS百科and retrieve audio and video files in and from a SQL Server database?The naive approach is to use a BLOB column and read the entire content into a byte[], then write the by

How can I save 开发者_JS百科and retrieve audio and video files in and from a SQL Server database?


The naive approach is to use a BLOB column and read the entire content into a byte[], then write the byte[] back to the client. The problem is that it consumes huge amounts of memory in your ASP.Net process.

A much better approach is to use streaming semantics, see these two articles:

  • Download and Upload images from SQL Server via ASP.Net MVC
  • FILESTREAM MVC: Download and Upload images from SQL Server

The articles refer to images, but you can use the code as-is to store any other form of media, including audio and video. Although the code is for ASP.Net MVC, the concepts use to stream large files into and from SQL Server can be used just as well from ASP.Net forms.

I'm not making a case that you should or should not use SQL Server as the storage for media. That is an entirely different discussion.


Refer to How to Store audio in Sql and retrieve for play, you come to know how to store the audio files in the database. Follow the same way for videos also.


    Public Sub InsertAudioDataInTable(TableName, TableMapingName, TextName,_ 
    LoopStart, LoopEnd, Panel, PathOnDisk)

    Dim SndSourceStream As Stream = New FileStream(PathOnDisk, FileMode.Open, 
    FileAccess.Read)
    Dim BinarySndReader As New BinaryReader(SndSourceStream)
    Dim bytes As Byte() = BinarySndReader.ReadBytes(SndSourceStream.Length)
    strImage = "@Aud" ' Aud mean a feild Audio in database
    '''''''''''''''''''''''''''''''''''''''''''''''' For SQL String
    Dim b = 0
    Dim dataContaner(LoopEnd + 2 - LoopStart) As String
    For a = LoopStart To LoopEnd
        dataContaner(b) = Panel.Controls(TextName & a).Text
        b = b + 1
    Next
    ''''''''''''''
    Dim myCmd As New SqlCommand
    myCmd.Connection = Con
    ''''''''''
    Dim T As String
    '  T.Text = null
    Dim aaa = "INSERT INTO  " & TableName
    Dim bbb = ""
    For i = LoopStart To LoopEnd
        bbb = bbb + "F" & i & ","
    Next
    T = aaa & " (" + bbb & "Aud"
    '  T = T.Remove(T.Length - 1)
    T = T & ")VALUES ("

    Dim ccc = ""
    b = 0
    For a = LoopStart To LoopEnd
        ccc = ccc & "'" & dataContaner(b) & "',"
        b = b + 1
    Next
    T = T + ccc
    myCmd.CommandText = T + strImage & ")"
    myCmd.Parameters.Add(strImage, SqlDbType.Binary).Value = bytes
    myCmd.ExecuteNonQuery()
    ''''''''
End Sub
   Private Sub btSclass_Click(sender As Object, e As EventArgs) Handles btSclass.Click
    If btSclass.Text = "Start Class" Then
        If A7.Text <> "" Then
            GlobalVariableDefault.startTime = DateTime.Now
            A2.Text = GlobalVariableDefault.startTime.ToString("hh\:mm\:ss")
            GlobalVariableDefault.StID = A7.Text
            Me.Hide()
            FTodayLesson.Show()
            A2.Text = L.Text
            btSclass.Text = "End Class"
            btSclass.BackColor = Color.Red
            If recording = False Then
                mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)
                mciSendString("record recsound", "", 0, 0)
                recording = True
            End If
        Else
            MyMessage("Select a student name from list")
        End If
    ElseIf btSclass.Text = "End Class" Then
        mciSendString("save recsound " & Filez, "", 0, 0)
        mciSendString("close recsound ", "", 0, 0)
        recording = False
        FunConnection(DatabaseName_Audio)
        InsertAudioDataInTable("RecAudio", "InsertAdio", "A", 1, 8, PShortInfo, "D:\aa\Test.wav")
        btSclass.BackColor = Color.WhiteSmoke
        btSclass.Text = "Start Class"
        MyMessage("Data Save")
        FunConnection(DatabaseName_QurqnServer)
    End If


End Sub

Save audio and video file in SQL Server database


'''''data base used in above code

Save audio and video file in SQL Server database

0

精彩评论

暂无评论...
验证码 换一张
取 消