如何通过VB.NET在sql server数据库中存储和检索图像 [英] How to store and retrieve images in sql server database through VB.NET

查看:130
本文介绍了如何通过VB.NET在sql server数据库中存储和检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL Server支持客户机在表中存储
对象的能力。

SQL Server supports the ability for clients to store objects within tables.

创建字段数据类型Image并初始化字节数组, 。使用FileInfo对象获取文件大小。打开FileStream读取文件。

Create Field that data type Image and Initialize byte array with a null value initially.Use FileInfo object to get file size.Open FileStream to read file.Use

推荐答案

以下是我为您的查询获得的第三个搜索结果。我知道如何做到这一点,但没有足够的耐心再做一次。因此,以下是来自 TechArena 的示例代码

Do a little googling before posting here. Below is the third search result I got for your query. I know how to do this, but not enough patience to do it again. So here is a sample code from TechArena


Function SaveImage _
   (ByVal FileName As String) As Integer

   ' Create a FileInfo instance
   ' to retrieve the files information
   Dim fi As New FileInfo(FileName)

   ' Open the Image file for Read
   Dim imgStream As Stream = _
      fi.OpenRead

   ' Create a Byte array the size
   ' of the image file for the Read
   ' methods buffer() byte array
   Dim imgData(imgStream.Length) As Byte
   imgStream.Read(imgData, 0, fi.Length)

   Dim cn As New SqlConnection _
      (ConfigurationSettings.AppSettings("cn"))
   Dim cmd As New SqlCommand("Images_ins", cn)
   cmd.CommandType = CommandType.StoredProcedure

   With cmd.Parameters
      .Add("@FileName", VarChar, 100).Value = _
         fi.Name
      .Add("@FileType", VarChar, 10).Value = +
         fi.Extension
      .Add("@Image", Image).Value = imgData
      .Add("@FileSize", Int, 4).Value = fi.Length
   End With

   cn.Open()
   cmd.ExecuteNonQuery()
   cn.Close()
   cn.Dispose()
End Function


这篇关于如何通过VB.NET在sql server数据库中存储和检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆