|
How to Save/Load Files like Pictures in/from Database
|
|
|
|
|
|
'Save
Dim ms As New MemoryStream
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim strType As String = ToolStripStatusLabel1.Text.Substring(ToolStripStatusLabel1.Text.LastIndexOf("."), 4)
Dim arrImage() As Byte = ms.GetBuffer
ms.Close()
Dim strFileName As String = IO.Path.GetFileName(ToolStripStatusLabel1.Text)
Dim connectionString As String = "Data Source=MIKEY-HP\jtsql;Initial Catalog=Images;Integrated Security=True"
Dim cnn As New SqlConnection(connectionString)
Dim DateFiled As Date = FormatDateTime(Now(), DateFormat.ShortDate)
Dim strSQL As String = _
"INSERT INTO ImageTab (ImageData,Type,ImgFileName,DateFiled)" & _
"VALUES (@ImageData,@Type,@Filename,'" & DateFiled & "')"
Dim cmd As New SqlCommand(strSQL, cnn)
With cmd
.Parameters.Add(New SqlParameter("@Filename", SqlDbType.VarChar, 50)).Value = strFileName
.Parameters.Add(New SqlParameter("@Type", SqlDbType.VarChar, 10)).Value = strType
.Parameters.Add(New SqlParameter("@ImageData", SqlDbType.Image)).Value = arrImage
End With
Try
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'Load
Try
Me.ImageTabTableAdapter.FillBy(Me.ImagesDataSet.ImageTab)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
|
|
|
|