Как вставить картинку в datagridview и преобразовать ее в базу данных VB.NET без использования параметра
Привет! У меня здесь есть код,который может добавлять,искать,удалять, очищать и вставлять изображение информации о пользователях. Проблема в том, что я не знаю, как и какой синтаксис я должен сделать, чтобы картинка добавилась в кнопку Добавить, чтобы сохранить ее в базе данных и показать картинку в виде таблицы данных вместе с информацией о пользователях...Я надеюсь и заранее благодарю людей, которые могут мне помочь ...Спасибо
P. S это единственное, чего мне не хватает в моем коде, все они исполняемы. Он уже может появиться в графическом окне, но я еще не подключился к своей базе данных, в чем и заключается проблема. Я вставлю сюда все коды, которые я уже сделал, надеюсь, это поможет.
Что я уже пробовал:
'Imports Microsoft.Win32 Imports System.Data.OleDb Imports System.IO 'Imports System 'Imports System.Data 'Imports System.Drawing 'Imports System.Drawing.Imaging 'Imports System.Windows.Forms 'Imports Microsoft.VisualBasic Imports System.DBNull Public Class ProjectTwo Dim cnn As New OleDb.OleDbConnection Dim imgName As String Dim cmd As New OleDb.OleDbCommand Private Sub ProjectTwo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load cnn = New OleDb.OleDbConnection cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=I:\ProjectTwo\ProjectTwoEmployment.accdbProvider=Microsoft.ACE.OLEDB.12.0;Data Source=I:\ProjectTwo\ProjectTwoEmployment.accdb" ' 'get data into list Me.RefreshData() End Sub Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click Dim cmd As New OleDb.OleDbCommand If Not cnn.State = ConnectionState.Open Then 'open connection if it is not yet open cnn.Open() End If cmd.Connection = cnn 'check whether add new or update If Me.TxtEID.Tag & "" = "" Then 'add new 'add data to table cmd.CommandText = "INSERT INTO PersonalInfo (EmploymentID, EmploymentName, DateOfBirth, PlaceOfBirth, Address , Phone , Sex) " & _ " VALUES(" & Me.TxtEID.Text & ",'" & Me.TxtEName.Text & "','" & Me.TxtEDate.Text & "','" & _ Me.TxtEPlace.Text & "','" & Me.TxtEAdd.Text & "','" & Me.TxtEPhone.Text & "','" & _ Me.TxtESex.Text & "')" cmd.ExecuteNonQuery() Else 'update data in table cmd.CommandText = "UPDATE PersonalInfo " & _ " SET EmploymentID =" & Me.TxtEID.Text & _ ", EmploymentName='" & Me.TxtEName.Text & "'" & _ ", DateOfBirth='" & Me.TxtEDate.Text & "'" & _ ", PlaceOfBirth='" & Me.TxtEPlace.Text & "'" & _ ", Address='" & Me.TxtEAdd.Text & "'" & _ ", Phone='" & Me.TxtEPhone.Text & "'" & _ ", Sex='" & Me.TxtESex.Text & "'" & _ " WHERE EmploymentID=" & Me.TxtEID.Tag cmd.ExecuteNonQuery() End If 'refresh data in list Me.RefreshData() 'clear form Me.BtnClear.PerformClick() 'close connection cnn.Close() End Sub Private Sub RefreshData() If Not cnn.State = ConnectionState.Open Then 'open connection cnn.Open() End If Dim da As New OleDb.OleDbDataAdapter("SELECT EmploymentID as [EmploymentID], " & _ "EmploymentName as [EmploymentName], DateOfBirth, PlaceOfBirth, Address, Phone , Sex , Photo" & _ " FROM PersonalInfo ORDER BY EmploymentID", cnn) Dim dt As New DataTable 'fill data to datatable da.Fill(dt) 'offer data in data table into datagridview Me.DataGridView1.DataSource = dt 'close connection cnn.Close() End Sub Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click Me.TxtEID.Text = "" Me.TxtEName.Text = "" Me.TxtEDate.Text = "" Me.TxtEPlace.Text = "" Me.TxtEAdd.Text = "" Me.TxtEPhone.Text = "" Me.TxtESex.Text = "" Me.PictureBox1.Image = Nothing Me.RefreshData() 'enable button edit Me.BtnUpdate.Enabled = True 'set button add to add label Me.BtnAdd.Text = "Add" ' Me.TxtEID.Focus() End Sub Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click 'check for the selected item in list If Me.DataGridView1.Rows.Count > 0 Then If Me.DataGridView1.SelectedRows.Count > 0 Then Dim intEID As Integer = Me.DataGridView1.SelectedRows(0).Cells("EmploymentID").Value 'get data from database followed by employment id 'open connection If Not cnn.State = ConnectionState.Open Then cnn.Open() End If 'get data into datatable Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM PersonalInfo " & _ " WHERE EmploymentID=" & intEID, cnn) Dim dt As New DataTable da.Fill(dt) Me.TxtEID.Text = intEID Me.TxtEName.Text = dt.Rows(0).Item("EmploymentName") Me.TxtEDate.Text = dt.Rows(0).Item("DateOfBirth") Me.TxtEPlace.Text = dt.Rows(0).Item("PlaceOfBirth") Me.TxtEAdd.Text = dt.Rows(0).Item("Address") Me.TxtEPhone.Text = dt.Rows(0).Item("Phone") Me.TxtESex.Text = dt.Rows(0).Item("Sex") ' 'hide the id to be edited in TAG of TxtEID in case id is changed Me.TxtEID.Tag = intEID 'change button add to update Me.BtnAdd.Text = "UPDATE" 'disable button edit Me.BtnUpdate.Enabled = False 'close connection cnn.Close() End If End If End Sub Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click 'check for the selected item in list If Me.DataGridView1.Rows.Count > 0 Then If Me.DataGridView1.SelectedRows.Count > 0 Then Dim intEID As Integer = Me.DataGridView1.SelectedRows(0).Cells("EmploymentID").Value 'open connection If Not cnn.State = ConnectionState.Open Then cnn.Open() End If 'delete data Dim cmd As New OleDb.OleDbCommand cmd.Connection = cnn cmd.CommandText = "DELETE FROM PersonalInfo WHERE EmploymentID=" & intEID cmd.ExecuteNonQuery() 'refresh data Me.RefreshData() 'close connection cnn.Close() End If End If End Sub Private Sub BtnPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPicture.Click Dim opf As New OpenFileDialog opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif" If opf.ShowDialog = DialogResult.OK Then PictureBox1.Image = Image.FromFile(opf.FileName) End If End Sub Private Sub BtnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSearch.Click 'for searching the record in the database Try 'open the connection cnn.Open() Dim data = New DataTable Dim dataT As New OleDbDataAdapter 'set your commands for holding the data With cmd .Connection = cnn .CommandText = "Select * from PersonalInfo where EmploymentID like '" & TxtEID.Text & "%'" End With 'filling the table in the database. dataT.SelectCommand = cmd dataT.Fill(data) 'put your datasource in the datagridview DataGridView1.DataSource = data Catch ex As Exception MsgBox(ex.Message) End Try 'close the connection cnn.Close() End Sub End Class