我想将图像上传到数据库,我在互联网上找到了很多代码,但没有一个对我有用。我正在使用格式(png、gif、jpeg 和 bmp)的按钮浏览图像浏览图像,然后我想使用按钮保存将这些类型的图像上传到数据库。
为了取回它们,我使用另一个按钮加载。你能指导我如何做到这一点。
这是我的编码告诉我我哪里错了。
Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Image
Public Class Employee
Dim myImage As Image
Dim imgMemoryStream As IO.MemoryStream = New IO.MemoryStream()
Dim imgByteArray As Byte() = Nothing
Dim con As SqlConnection = New SqlConnection("Data Source=CILENTEYEZ-PC\CILENTEYEZ;Initial Catalog=Keeper;Integrated Security=True")
Dim cmd As SqlCommand
Dim myDA As SqlDataAdapter
Dim myDataSet As DataSet
Dim dr As SqlDataReader
' From Here This is the Code for Insertion.
Private Sub BrowsePic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowsePic.Click
Dim fd As OpenFileDialog = New OpenFileDialog()
fd.Title = "Select your Image."
fd.InitialDirectory = "C:\"
fd.Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg|PNGs|*.png"
fd.RestoreDirectory = False
If fd.ShowDialog() = DialogResult.OK Then
PictureBox.ImageLocation = fd.FileName
ElseIf fd.FileName.Contains("jpeg") Or fd.FileName.Contains("jpg") Then
myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
imgByteArray = imgMemoryStream.GetBuffer()
ElseIf fd.FileName.Contains("png") Then
myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Png)
imgByteArray = imgMemoryStream.GetBuffer()
ElseIf fd.FileName.Contains("gif") Then
myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Gif)
imgByteArray = imgMemoryStream.GetBuffer()
ElseIf fd.FileName.Contains("bmp") Then
myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Bmp)
imgByteArray = imgMemoryStream.GetBuffer()
End If
End Sub
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
cmd = New SqlCommand("Insert Into Employee Values('" & SR_CodeTextBox.Text.Trim() & "','" & NameTextBox.Text.Trim() & "','" & CNICTextBox.Text.Trim() & "','" & Date_of_BirthDateTimePicker.Text & "','" & AgeTextBox.Text.Trim() & "','" & AddressTextBox.Text.Trim() & "',@ imgByteArray ,'" & Mobile_NumberTextBox.Text.Trim() & "')", con)
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("New Employee Record is Added.", "Message", MessageBoxButtons.OK)
End Sub
' From Here This is the Code for Eiditing.
Private Sub Go_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Go.Click
cmd = New SqlCommand("Select * from Employee where SR_Code = '" & SearchBox.Text.Trim() & "'", con)
If SearchBox.Text = "" Then
MsgBox("Please enter SR Code first.")
Else
Try
If con.State = ConnectionState.Closed Then con.Open()
dr = cmd.ExecuteReader()
If dr.HasRows = True Then
dr.Read()
Edit_SR_CodeTextBox.Text = dr.Item("SR_Code")
Edit_NameTextBox.Text = dr.Item("Name")
Edit_CNICTextBox.Text = dr.Item("CNIC")
Edit_Date_of_BirthDateTimePicker.Text = dr.Item("Date_of_Birth")
Edit_AgeTextBox.Text = dr.Item("Age")
Edit_AddressTextBox.Text = dr.Item("Address")
imgByteArray = dr.Item("Picture")
Edit_Mobile_NumberTextBox.Text = dr.Item("Mobile_Number")
imgMemoryStream = New IO.MemoryStream(imgByteArray)
myImage = Drawing.Image.FromStream(imgMemoryStream)
PictureBox.Image = myImage
MsgBox("Record Retrieved.")
con.Close()
ElseIf dr.Read = False Then
MsgBox("Please enter a valid SR Code to load data.")
con.Close()
End If
Catch ex As Exception
MessageBox.Show(ex, "Message")
End Try
End If
End Sub