vb.net 数据库上传图片和下载图片

    Public Function 读取图片(id) As Image
        If IsDBNull(id) OrElse id = "" Then
            Return Nothing
        End If
        Dim cmds As New SqlCommand($"select img from 图片表 where id='{id}'", sql)
        Dim dr = cmds.ExecuteReader
        If dr.Read = False Then
            dr.Close()
            Return Nothing
        End If
        Dim imagebytes As Byte() = dr.GetValue(0)
        dr.Close()
        cmds.Dispose()
        Dim MS As MemoryStream = New MemoryStream(imagebytes)
        Return New Bitmap(MS)
    End Function
    Public Function 上传图片(bmp As Image) As String
        Try
            Dim id = Guid.NewGuid.ToString
            Dim t = New SqlCommand($"insert into 图片表 values(N'{id}',@ImageList)", sql)
            t.Parameters.Add("ImageList", SqlDbType.Image)
            Dim ms As MemoryStream = New MemoryStream()
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
            t.Parameters("ImageList").Value = ms.GetBuffer
            t.ExecuteNonQuery()
            ms.Close()
            Return id
        Catch ex As Exception
            Return ""
        End Try
    End Function
        CREATE TABLE [dbo].[图片表] (
        [Id]  NVARCHAR(50) Not NULL,
        [img] IMAGE         NULL,
        PRIMARY KEY CLUSTERED ([Id] ASC)
    );

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值