| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1293 人关注过本帖
标题:往SQL Server数据库中添加图片(VB.NET)
只看楼主 加入收藏
Pzjf
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-6-1
收藏
 问题点数:0 回复次数:1 
往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
hpu02zdh
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-5-12
收藏
得分:0 

也用的这个代码
我用的是2005版本的编程软件 可是有一句sqlCmd.Parameters.Add("@IMAGE", buffer)出现警告,请高手指点
Private Sub InsertData()
Dim IDItem(2) As String
IDItem = CmbStuClass.Text.Split(":"c)

Dim StuID As Int16 = Int16.Parse(TxtStuID.Text.Trim)
Dim StuName As String = TxtStuName.Text.Trim
Dim StuSex As String = CmbStuSex.Text.Trim
Dim StuClass As String = IDItem(0)
Dim StuBirthday As Date = DateStuBir.Value
Dim StuCard As String = TxtPerCard.Text
Dim StuPhoto As String = LblPhotoPath.Text.Trim


Dim MyStream As New FileStream(StuPhoto, FileMode.Open, FileAccess.Read)
Dim buffer(MyStream.Length) As Byte
MyStream.Read(buffer, 0, MyStream.Length)
MyStream.Close()


Dim value As String = StuID.ToString + ",'" + StuName + "','" + StuSex + "'," + StuClass + ",'" + StuBirthday + "','" + StuCard + "'"

Dim sql As String = "INSERT INTO STUDENTINFO (STUDENTID, STUDENTNAME,SEX,CLASSID,BIRTHDAY,PERSONCARDID,STUDENTPHOTO) VALUES(" + value + ", @IMAGE)"

Dim sqlCmd As New SqlCommand
sqlCmd.CommandText = sql
sqlCmd.Parameters.Add("@IMAGE", buffer)
sqlCmd.Connection = SqlCon

Try
SqlCon.Open()
sqlCmd.ExecuteNonQuery()
MessageBox.Show("添加记录成功")
Catch ex As Exception
MessageBox.Show("添加记录失败")
WriteErr(ex)
Finally
SqlCon.Close()
End Try
BindData()
End Sub

2007-06-07 11:54
快速回复:往SQL Server数据库中添加图片(VB.NET)
数据加载中...
 
   



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

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