vb.net 通过 vb 2010 将任何格式的图像上传/检索到 SQL 服务器数据中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19726655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uploading/Retrieving an image of any format into SQL server data through vb 2010
提问by Habib
I want to upload an image to data base, I found lots of code over the internet but non of them worked for me. I am browsing images with button Browse of format(png, gif, jpeg and bmp) and after then I want to upload these types of images to the data base with button Save.
For retrieving them back i use another button load. Can you please guide me how would i do this.
This is my coding Tell me where am I wrong.
我想将图像上传到数据库,我在互联网上找到了很多代码,但没有一个对我有用。我正在使用按钮浏览格式(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
采纳答案by Habib
For image browsing you can use this code.
对于图像浏览,您可以使用此代码。
Private Sub BrowsePic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowsePic.Click
Dim ImageDialogue As OpenFileDialog = New OpenFileDialog()
ImageDialogue.Title = "Select your Image."
ImageDialogue.InitialDirectory = "C:\"
ImageDialogue.Filter = "Image Files|*.gif;*.jpg;*.png;*.bmp;"
ImageDialogue.RestoreDirectory = False
If ImageDialogue.ShowDialog() = DialogResult.OK Then
PictureBox.Image = Image.FromFile(ImageDialogue.FileName)
End If
End Sub
For Image Uploading(into Database) you can use this code in VB 2010.
对于图像上传(到数据库),您可以在 VB 2010 中使用此代码。
cmd = New SqlCommand("Insert Into Proposer Values(& @Picture )", con)
If con.State = ConnectionState.Closed Then con.Open()
Dim ms As New MemoryStream()
PictureBox.Image.Save(ms, PictureBox.Image.RawFormat)
Dim data As Byte() = ms.GetBuffer()
Dim p As New SqlParameter("@Picture", SqlDbType.Image)
p.Value = data
cmd.Parameters.Add(p)
cmd.ExecuteNonQuery()
con.Close()
For Image retrieving(from Database) I used this Code in VB 2010.
对于图像检索(从数据库),我在 VB 2010 中使用了此代码。
Dim data As Byte() = DirectCast(dr("Picture"), Byte())
Dim ms As New MemoryStream(data)
Edt_PictureBox.Image = Image.FromStream(ms)
Note: This to be used in VB-2010 not tried on VB-2008. And As you see the code is a bit different from C#, so don't expect it to work on C#.
I you could make changes(mean conversion to C#) then it'll work.
注意:这将在 VB-2010 中使用,未在 VB-2008 上尝试过。正如您所看到的,代码与 C# 有点不同,所以不要指望它可以在 C# 上运行。
我可以进行更改(意味着转换为 C#)然后它会起作用。

