How to store and retrieve images in sql server database through VB.NET

后端 未结 2 1675
不思量自难忘°
不思量自难忘° 2020-12-20 03:22

SQL Server supports the ability for clients to store objects within tables.

Create Field that data type Image and Initialize byte array with a null value initially.U

2条回答
  •  梦毁少年i
    2020-12-20 03:57

    Do a little googling before posting here. Below is the third search result I got for your query. I know how to do this, but not enough patience to do it again. So here is a sample code from TechArena

    Function SaveImage _
       (ByVal FileName As String) As Integer
    
       ' Create a FileInfo instance
       ' to retrieve the files information
       Dim fi As New FileInfo(FileName)
    
       ' Open the Image file for Read
       Dim imgStream As Stream = _
          fi.OpenRead
    
       ' Create a Byte array the size
       ' of the image file for the Read
       ' methods buffer() byte array
       Dim imgData(imgStream.Length) As Byte
       imgStream.Read(imgData, 0, fi.Length)
    
       Dim cn As New SqlConnection _
          (ConfigurationSettings.AppSettings("cn"))
       Dim cmd As New SqlCommand("Images_ins", cn)
       cmd.CommandType = CommandType.StoredProcedure
    
       With cmd.Parameters
          .Add("@FileName", VarChar, 100).Value = _
             fi.Name
          .Add("@FileType", VarChar, 10).Value = +
             fi.Extension
          .Add("@Image", Image).Value = imgData
          .Add("@FileSize", Int, 4).Value = fi.Length
       End With
    
       cn.Open()
       cmd.ExecuteNonQuery()
       cn.Close()
       cn.Dispose()
    End Function
    

提交回复
热议问题