注册 登录
编程论坛 VB.NET论坛

请问各位高手vb.net如何把图片存入数据库中,然后又怎么读出呢?

zsdqxzxx 发布于 2017-02-16 17:51, 2969 次点击
请问各位高手如何把图片存入数据库中,然后又怎么读出呢?

[此贴子已经被作者于2017-2-16 17:55编辑过]

5 回复
#2
xzlxzlxzl2017-02-16 20:32
如果是用普通的ado操作数据库,题主可百度下,有好多这类代码;如果是用.net的linq,我也不知道怎么弄。
#3
zbjzbj2017-02-16 22:08
据我所知,sqlite 可以存储图片。存储格式为base64变换后的文本。vc资源也有采用这种方法存储图片。
#4
mybells2017-03-04 21:34


[此贴子已经被作者于2017-3-4 21:35编辑过]

#5
ztsjznc2017-12-19 15:02
存入数据库
  Select Case Me.Text
                Case "员工证件记录_新增"
                    If IsPicExist = True Then
                        Dim Data(fs.Length) As Byte
                        fs.Read(Data, 0, Int(fs.Length))
                        Dim Sql As String
                        Sql = "Select * from 证件记录"
                        Dim cmd As New SqlClient.SqlCommand(Sql, cn)
                         = "Insert Into 证件记录 (身份证号,姓名,证件名称,取证日期,证件照片) Values (@IDcard,@txtname,@PicName,@date,@photo)"
                        cmd.Connection = cn
                        Dim prm1 As New SqlParameter("@IDcard", str1)
                        Dim prm2 As New SqlParameter("@txtname", str2)
                        Dim prm3 As New SqlParameter("@PicName", str3)
                        Dim prm4 As New SqlParameter("@date", date1)
                        Dim prm5 As New SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, Data)
                        cmd.Parameters.Add(prm1)
                        cmd.Parameters.Add(prm2)
                        cmd.Parameters.Add(prm3)
                        cmd.Parameters.Add(prm4)
                        cmd.Parameters.Add(prm5)
                        cmd.ExecuteNonQuery()
                        MsgBox("添加证件记录成功!")
                        fs.Dispose()
读出数据
 Public Sub FillDialog()
        Try
            
             '打开连接
            Dim sql As String
            sql = "Select * From 证件记录 where ID =@ID"
            Dim cmd As New SqlClient.SqlCommand(sql, cn)
            Dim prm As New SqlParameter("@ID", DataGridViewId)
            cmd.Parameters.Add(prm)
            Dim da As New SqlClient.SqlDataAdapter(cmd) '定义sqldataadapter并于sqlcommand连接  
            Dim ds As New DataSet
            
            da.Fill(ds, "证件记录")
            If ds.Tables("证件记录").Rows.Count <> 0 Then
                If ds.Tables("证件记录").Rows(0)("证件照片") Is DBNull.Value Then
                    PictureBox1.Image = Nothing
                Else
                    Dim data() As Byte
                    data = ds.Tables("证件记录").Rows(0)("证件照片")
                    Dim stmphoto As New MemoryStream(data)
                    PictureBox1.Image = Image.FromStream(stmphoto)
                    TextBox1.Text = ds.Tables("证件记录").Rows(0)("身份证号").ToString
                    txtname.Text = ds.Tables("证件记录").Rows(0)("姓名").ToString
                    cmbPicName.Text = ds.Tables("证件记录").Rows(0)("证件名称").ToString
                    DateTimePicker1.Value = ds.Tables("证件记录").Rows(0)("取证日期").ToString
                End If
            End If
        Catch err As System.Exception
            MsgBox(err.Message, vbOKOnly + vbCritical, "出错信息提示")
        End Try
    End Sub
#6
jimei2020-09-28 15:53
回复 5楼 ztsjznc
想问一下    Dim Data(fs.Length) As Byte 中的  fs是怎么定义的  ,因为复制粘贴 发现未定义,另外 你的是 将图片是从文件路径中加载 上传到数据库 ,如果是 要将编辑过的图片存入数据库。也就是winform里 picturebox1控件里
显示的图片  如何上传到数据库中  。不知道如何操作
1