学习首页 百科 人生课堂 办公软件 英语学习 操作系统 故事会 编程资料 软件学习 设计
铭瑶网 >> 学习首页 >> 软件 >> 往SQL Server数据库中添加图片(VB.NET)
标题:往SQL Server数据库中添加图片(VB.NET)

【字体: 】 时间:2008-4-11 来源:互联网 作者:study

往SQL Server数据库中添加图片(VB.NET)

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

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

查看/参与:讨论/评论 相关文章:Vb.net