标题:往SQL Server数据库中添加图片(VB.NET)
取消只看楼主
Pzjf
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-6-1
 问题点数:0 回复次数:0 
往SQL Server数据库中添加图片(VB.NET)

往SQL Server数据库中添加图片(VB.NET) 原文地址:http://dotnet.aspx.cc/ShowDetail.aspx?id=J9UBRVER-L3VB-49M3-GOU1-Z6C2PVR6FZ3K

我把关键的部分取了出来,并作了相关的修改:

PictureBox1是指一个PictureBox控件

name是varchar(50)类型 image是image类型

Private Sub InsertImage()Sub InsertImage() '把图片插入到数据库中 Dim MyStream As New FileStream("C:Inetpubwwwrootwin2000.gif", FileMode.Open, FileAccess.Read) Dim buffer(MyStream.Length) As Byte MyStream.Read(buffer, 0, MyStream.Length) MyStream.Close()

Dim SqlConnectionString As String = "Persist Security Info=False;Data Source=.;Initial Catalog=aa;User ID=sa;Password=" Dim MyConnection As New SqlConnection(SqlConnectionString) Dim MyCommand As New SqlCommand("INSERT INTO Photos ([name], [photo]) VALUES (@name, @image)", MyConnection) MyCommand.Parameters.Add("@name", "test") MyCommand.Parameters.Add("@image", buffer) MyConnection.Open() MyCommand.ExecuteNonQuery() MyConnection.Close() End Sub

Private Sub ShowImage()Sub ShowImage() '从数据库中取出图片 Dim SqlConnectionString As String = "Persist Security Info=False;Data Source=.;Initial Catalog=aa;User ID=sa;Password=" Dim MyConnection As New SqlConnection(SqlConnectionString) MyConnection.Open() Dim MyCommand As New SqlClient.SqlCommand("SELECT photo FROM Photos WHERE [name]='test'", MyConnection) Dim buffer() As Byte = MyCommand.ExecuteScalar() If (Not buffer Is Nothing) AndAlso (buffer.Length > 0) Then Dim stream As New MemoryStream(buffer, 0, buffer.Length, True) PictureBox1.Image = New Bitmap(stream) stream.Close() End If MyConnection.Close() End Sub

搜索更多相关主题的帖子: SQL 数据库 NET Server 
2005-06-01 17:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-19858-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.181359 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved