SQL Server支持客户端存储的功能 表格中的对象。
创建数据类型Image的字段和最初使用空值初始化字节数组。使用FileInfo对象获取文件大小。打开FileStream读取文件。使用
答案 0 :(得分:4)
在发布之前先做一些谷歌搜索。以下是我为您的查询获得的第三个搜索结果。我知道怎么做,但没有足够的耐心再做一次。以下是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
答案 1 :(得分:2)
SQL Server中的图像字段只是一个字节数组。这是您需要的重要代码。我们假设数据库中图像字段的名称是“imageField”。希望这可以帮助。
要检索图像并将其保存到磁盘:
//dr is a DataReader returned from a SELECT command
Dim imageInBytes As Byte() = dr("imagefield")
Dim memoryStream As System.IO.MemoryStream = _
New System.IO.MemoryStream(imageInBytes, False)
Dim image As System.Drawing.Image = _
System.Drawing.Image.FromStream(memoryStream)
image.Save("c:\image")
将映像从磁盘保存到SQL Server:
''Get the image file into a Byte Array
Dim image As Byte() = System.IO.File.ReadAllBytes("c:\image.jpg")
''Add the byte array as a parameter to your Insert/Update SQLCommand
parameters.Add("@ImageField", image)