[英]How to save an Image from a PictureBox to my database?
private Sub Button1_Click(ByVal sender As System. Object, ByVal e As System.EventArgs) Handles Button1.Click
If conn.State = ConnectionState.Open Then
conn.Close()
End If
If Trim(txtcode.Text) = "" Then
MsgBox("Please enter the unique Member code")
txtcode.Focus()
Exit Sub
ElseIf IsNumeric(txtName_Surname.Text) Then
MsgBox("Please replace numbers with text", MessageBoxIcon.Stop)
txtName_Surname.Focus()
Exit Sub
Else
Try
Dim strinsert As String
Dim cmdinsert As OleDb.OleDbCommand
Dim Passport As String = Application.ExecutablePath
strinsert = "insert into book_mast (Mem_code,Name_Surname,Date_of_Birth,Gender,Telephone_number,Age,Status,Date_of_Baptism,House_No,Occupation,Education,Office_Name,Tel,Office_No,Tithe_per_month,Total_tithe_per_year,Born_Again,BA_Time,Baptizism,Baptizism_where,Join_RCCG,Join_Where,Baptizism_Ghost,BPG_When,Tongues,Tongues_When, Passport) values(@txtcode, @txtName_Surname, @d1, @txtgen, @txtTelephone_number, @txtage, @CbBStatus, @d2, @txthno, @TxtOccupation, @CbBEducation, @TxtOffName, @TxtTel, @txtOno, @txtTithe_per_month, @txtTotal_tithe_per_year, @TxtBornAgain, @TxtBornwhen, @CbBbaptizism, @TxtBaptizedwhere, @TxtJoinRCCG, @TxtJoinWhere, @TxtHolyGhost, @TxtGhostWhen, @TxtTongues, @TxtTonguesWhen, @Passport)"
cmdinsert = New OleDb.OleDbCommand(strinsert, conn)
conn.Open()
cmdinsert.ExecuteNonQuery()
MsgBox("Record Has been added Successfully")
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
clearall()
fillgridmem()
End Try
End If
End Sub
I have created a small program to help you understand.我创建了一个小程序来帮助您理解。 In this case i have created a database in access but you can use the database you have.
在这种情况下,我在访问中创建了一个数据库,但您可以使用您拥有的数据库。
Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\Data.mdb"
Dim imgName As String
Dim daImage As OleDbDataAdapter
Dim dsImage As DataSet
Private Sub FillCombo()
Try
Dim CN As New OleDbConnection(cnString)
CN.Open()
daImage = New OleDbDataAdapter()
daImage.SelectCommand = New OleDbCommand("SELECT * FROM images", CN)
dsImage = New DataSet("dsImage")
daImage.Fill(dsImage)
Dim dtable As DataTable
dtable = dsImage.Tables(0)
cboImageID.Items.Clear()
For Each drow As DataRow In dtable.Rows
cboImageID.Items.Add(drow(0).ToString())
cboImageID.SelectedIndex = 0
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Try
Dim dlgImage As FileDialog = New OpenFileDialog()
dlgImage.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif"
If dlgImage.ShowDialog() = DialogResult.OK Then
imgName = dlgImage.FileName
Dim newimg As New Bitmap(imgName)
imgSave.SizeMode = PictureBoxSizeMode.StretchImage
imgSave.Image = DirectCast(newimg, Image)
End If
dlgImage = Nothing
Catch ae As System.ArgumentException
imgName = " "
MessageBox.Show(ae.Message.ToString())
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If imgName <> "" Then
Dim fs As FileStream
fs = New FileStream(imgName, FileMode.Open, FileAccess.Read)
Dim picByte As Byte() = New Byte(fs.Length - 1) {}
fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length))
fs.Close()
Dim CN As New OleDbConnection(cnString)
CN.Open()
Dim strSQL As String
strSQL = "INSERT INTO Images([Image]) values (" & " @Img)"
Dim imgParam As New OleDbParameter()
imgParam.OleDbType = OleDbType.Binary
imgParam.ParameterName = "Img"
imgParam.Value = picByte
Dim cmd As New OleDbCommand(strSQL, CN)
cmd.Parameters.Add(imgParam)
cmd.ExecuteNonQuery()
MessageBox.Show("Image successfully saved.")
cmd.Dispose()
CN.Close()
CN.Dispose()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
FillCombo()
End Sub
Private Sub btnRetrieve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrieve.Click
Dim dataTable As DataTable = dsImage.Tables(0)
If imgRetrieve.Image IsNot Nothing Then
imgRetrieve.Image.Dispose()
End If
Dim fsImage As New FileStream("image.jpg", FileMode.Create)
For Each dataRow As DataRow In dataTable.Rows
If dataRow(0).ToString() = cboImageID.SelectedItem.ToString() Then
Dim blob As Byte() = DirectCast(dataRow(1), Byte())
fsImage.Write(blob, 0, blob.Length)
fsImage.Close()
fsImage = Nothing
imgRetrieve.Image = Image.FromFile("image.jpg")
imgRetrieve.SizeMode = PictureBoxSizeMode.StretchImage
imgRetrieve.Refresh()
End If
Next
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillCombo()
End Sub
If this does not solve your problem or give any errors let me know I will look into it.如果这不能解决您的问题或给出任何错误,请告诉我,我会调查它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.