zoukankan      html  css  js  c++  java
  • 往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() '把图片插入到数据库中
            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() '从数据库中取出图片
            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 NothingAndAlso (buffer.Length > 0Then
                
    Dim stream As New MemoryStream(buffer, 0, buffer.Length, True)
                PictureBox1.Image 
    = New Bitmap(stream)
                stream.Close()
            
    End If
            MyConnection.Close()
        
    End Sub
  • 相关阅读:
    https原理:证书传递、验证和数据加密、解密过程解析
    java web项目的https配置
    防止表单重复提交的八种简单有效的策略
    nginx.conf
    Java打war包or打jar包
    WarUtil
    MyBatis动态SQL第一篇之实现多条件查询(if、where、trim标签)
    Spring使用注解实现AOP
    Spring添加声明式事务
    spring配置文件拆分策略及方法
  • 原文地址:https://www.cnblogs.com/modou/p/26896.html
Copyright © 2011-2022 走看看