⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pictureform1.vb

📁 是一个用SQL和VB.NET来做的源码。
💻 VB
字号:
Imports System.Data.SqlClient
Public Class PictureForm1
    Inherits System.Windows.Forms.Form

#Region " Windows Form 设计工具产生的程序代码 "

    Public Sub New()
        MyBase.New()

        '此调用为 Windows Form 设计工具的必要项。
        InitializeComponent()

        '在 InitializeComponent() 调用之后加入所有的初始设定

    End Sub

    'Form 覆盖 Dispose 以清除组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    '为 Windows Form 设计工具的必要项
    Private components As System.ComponentModel.IContainer

    '注意: 以下为 Windows Form 设计工具所需的程序
    '您可以使用 Windows Form 设计工具进行修改。
    '请勿使用程序代码编辑器来修改这些程序。
    Friend WithEvents btnLoadFromFile As System.Windows.Forms.Button
    Friend WithEvents btnSaveToFile As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnLoadFromFile = New System.Windows.Forms.Button()
        Me.btnSaveToFile = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'btnLoadFromFile
        '
        Me.btnLoadFromFile.Location = New System.Drawing.Point(368, 64)
        Me.btnLoadFromFile.Name = "btnLoadFromFile"
        Me.btnLoadFromFile.Size = New System.Drawing.Size(128, 23)
        Me.btnLoadFromFile.TabIndex = 0
        Me.btnLoadFromFile.Text = " Load from file"
        '
        'btnSaveToFile
        '
        Me.btnSaveToFile.Location = New System.Drawing.Point(368, 32)
        Me.btnSaveToFile.Name = "btnSaveToFile"
        Me.btnSaveToFile.Size = New System.Drawing.Size(128, 23)
        Me.btnSaveToFile.TabIndex = 1
        Me.btnSaveToFile.Text = "Save to file"
        '
        'PictureForm1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 18)
        Me.ClientSize = New System.Drawing.Size(704, 504)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSaveToFile, Me.btnLoadFromFile})
        Me.Name = "PictureForm1"
        Me.Text = "PictureForm1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnSaveToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveToFile.Click
        SqlBlob2File("c:\temp\copyoftestfile.dat")
    End Sub

    Private Sub btnLoadFromFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadFromFile.Click
        File2SqlBlob("c:\temp\copyoftestfile.dat")
    End Sub

    Private Sub SqlBlob2File(ByVal DestFilePath As String)
        Dim PictureCol As Integer = 0 ' the column # of the BLOB field
        Dim cn As New SqlConnection("server=localhost;integrated security=SSPI;database=NorthWind")
        Dim cmd As New SqlCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn)
        cn.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        dr.Read()
        Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
        dr.GetBytes(PictureCol, 0, b, 0, b.Length)
        dr.Close()
        cn.Close()
        Dim fs As New System.IO.FileStream(DestFilePath, IO.FileMode.Create, IO.FileAccess.Write)
        fs.Write(b, 0, b.Length)
        fs.Close()
    End Sub

    Private Sub File2SqlBlob(ByVal SourceFilePath As String)
        Dim cn As New SqlConnection("server=localhost;integrated security=SSPI;database=NorthWind")
        Dim cmd As New SqlCommand("UPDATE Categories SET Picture=@Picture WHERE CategoryName='Test'", cn)
        Dim fs As New System.IO.FileStream(SourceFilePath, IO.FileMode.Open, IO.FileAccess.Read)
        Dim b(fs.Length() - 1) As Byte
        fs.Read(b, 0, b.Length)
        fs.Close()
        Dim P As New SqlParameter("@Picture", SqlDbType.Image, b.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, b)
        cmd.Parameters.Add(P)
        cn.Open()
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -