当前位置: 首页 > 图文教程 > 数据库 > MSSQL > 用VB6读写数据库中的图片

MSSQL
SQL Server:SQL Server Maintenance Plan Wizard
SQL Server:SQL mai的配置和使用,你有所研究吗?
SQLServer:数据库的定时作业设置(经典)
SQL Server:小编浅谈SQL全文本检索方法
SQL Server:小编经验谈设计数据库表和字段
SQL Server:小编浅谈数据库完整性之约束
SQL Server:容易忽视的动态约束
SQL Server:时态数据库的时间间隔
SQL Server:浅谈数据库的安全性
SQL Server:不得不看的数据库设计技巧(看了终身受益)
SQL Server:数据库中的快照
SQL Server:数据库管理中关系代数的语法
SQL Server:关系代数中的语义
验证SQL保留字
精妙的SQL语句
SQL Server 数据库管理常用的SQL和T-SQL语句
SQL SERVER 与ACCESS、EXCEL的数据转换
SQL SERVER的数据类型
未公开的SQL Server口令的加密函数
SQl 语句(常见)

MSSQL 中的 用VB6读写数据库中的图片


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 38 ::
收藏到网摘: n/a

  很多兄弟在这里问关于VB6读写数据库中的图片的问题,在此有一例,希有所启发。
   1,以人名和相关图片为例说明,数据库为Access,有如下字段:Name char,picture OLE object,FileLength Number。当为ms sql时,将picture改为lob即可。
   2,示例包含control:commom dialog,picture,listbox。
源码如下:
Option Explicit

Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH = 260

Private m_DBConn As ADODB.Connection

Private Const BLOCK_SIZE = 10000
' Return a temporary file name.
Private Function TemporaryFileName() As String
Dim temp_path As String
Dim temp_file As String
Dim length As Long

    ' Get the temporary file path.
    temp_path = Space$(MAX_PATH)
    length = GetTempPath(MAX_PATH, temp_path)
    temp_path = Left$(temp_path, length)

    ' Get the file name.
    temp_file = Space$(MAX_PATH)
    GetTempFileName temp_path, "per", 0, temp_file
    TemporaryFileName = Left$(temp_file, InStr(temp_file, Chr$(0)) - 1)
End Function
Private Sub Form_Load()
Dim db_file As String
Dim rs As ADODB.Recordset

    ' Get the database file name.
    db_file = App.Path
    If Right$(db_file, 1) <> "\" Then db_file = db_file & "\"
    db_file = db_file & "dbpict.mdb"

    ' Open the database connection.
    Set m_DBConn = New ADODB.Connection
    m_DBConn.Open _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & db_file & ";" & _
        "Persist Security Info=False"

    ' Get the list of people.
    Set rs = m_DBConn.Execute("SELECT Name FROM People ORDER BY Name", , adCmdText)
    Do While Not rs.EOF
        lstPeople.AddItem rs!Name
        rs.MoveNext
    Loop

    rs.Close
    Set rs = Nothing
End Sub
Private Sub Form_Resize()
    lstPeople.Height = ScaleHeight
End Sub


' Display the clicked person.
Private Sub lstPeople_Click()
Dim rs As ADODB.Recordset
Dim bytes() As Byte
Dim file_name As String
Dim file_num As Integer
Dim file_length As Long
Dim num_blocks As Long
Dim left_over As Long
Dim block_num As Long
Dim hgt As Single

    picPerson.Visible = False
    Screen.MousePointer = vbHourglass
    DoEvents

    ' Get the record.
    Set rs = m_DBConn.Execute("SELECT * FROM People WHERE Name='" & _
        lstPeople.Text & "'", , adCmdText)
    If rs.EOF Then Exit Sub

    ' Get a temporary file name.
    file_name = TemporaryFileName()

    ' Open the file.
    file_num = FreeFile
    Open file_name For Binary As #file_num

    '