[英]Adding image from DataGridViewImageColumn to database in windows application
I am working on vb.net windows application..i am populating my DataGridView
like this.i wrote code in my form load
event like this: 我正在vb.net Windows应用程序上工作..我正在这样填充我的
DataGridView
。我在表单load
事件中这样编写了代码:
Dim cd As SqlCommandBuilder = New SqlCommandBuilder(adapter)
adapter = New SqlDataAdapter("select c.cid,c.CompanyName,d.dtId,d.dtName as Department,d.dtPhone as Phone,d.dtEmail as Email from CompanyMaster_tbl c join DepartmentMaster_tbl d on c.Cid=d.cId order by cid", con.connect)
dt1 = New DataTable
bSource = New BindingSource
adapter.Fill(dt1) 'Filling dt with the information from the DB
bSource.DataSource = dt1
gv.DataSource = bSource
gv.Columns("cid").Visible = False
gv.Columns("dtId").Visible = False
Dim img As New DataGridViewImageColumn
img.HeaderText = "image"
gv.Columns.Insert(6, img)
then in cell content click i wrote code like this for uploading image: 然后在单元格内容中单击我编写了如下代码以上传图片:
If e.ColumnIndex = 6 Then
Dim OFDLogo As New OpenFileDialog()
OFDLogo.Filter = "JPEG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp"
If OFDLogo.ShowDialog() = DialogResult.OK Then
gv.Rows(e.RowIndex).Cells(6).Value = Image.FromFile(OFDLogo.FileName)
End If
End If
in save button i am saving my department details like this: 在保存按钮中,我正在保存我的部门详细信息,如下所示:
For i As Integer = 0 To gv.RowCount - 2
sqlInsertT2 = "Insert Into DepartmentMaster_tbl(dtname,dtphone,dtEmail,Cid) Values ('" + myTI.ToTitleCase(gv.Rows(i).Cells(3).Value) + "','" + gv.Rows(i).Cells(4).Value + "','" + gv.Rows(i).Cells(5).Value + "'," & Ccid & ");"
Next
i have one more field in Department master table..field Name: empimage
and datatype image
..i want to save corresponding image to this table.how i can save image from my data grid view image column to database. 我在部门主表中还有一个字段。字段名称:
empimage
和数据类型image
..我想将对应的图像保存到此表中。如何将图像从数据网格视图图像列保存到数据库中。
my DataGridView
look like this: 我的
DataGridView
看起来像这样:
Try something like this 试试这个
Dim sql As String = "INSERT INTO Information VALUES(@name,@photo)"
Dim cmd As New SqlCommand(sql, con)
cmd.Parameters.AddWithValue("@name", "DepartmentName")
Dim ms As New MemoryStream()
Dim imgCon As New ImageConverter
ms.Read(imgCon.ConvertTo(DataGridView1.Rows(0).Cells(4).Value, GetType(Byte())), 0, 1024)
Dim data As Byte() = ms.GetBuffer()
Dim p As New SqlParameter("@photo", SqlDbType.Image)
p.Value = data
cmd.Parameters.Add(p)
cmd.ExecuteNonQuery()
MessageBox.Show("Name & Image has been saved", "Save", MessageBoxButtons.OK)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.