AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VB编程

利用Vb保存一幅图到Access数据库

51自学网 http://www.wanshiok.com

   这段代码可以放在form_load事件中,当做一个多窗口的系统时,最好放到一个模块文件中,这样在其它窗口中都可以调用这个cn连接。

   下面是具体的窗口代码:

    VERSION 5.00

    Object= "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"

    Object= "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "richtx32.ocx"

    Begin VB.Form finput

    BorderStyle = 0 'None

    Caption = "文档输入"

    ClientHeight = 6240

    ClientLeft = 0

    ClientTop = 0

    ClientWidth = 8955

    ControlBox = 0 'False

    LinkTopic = "Form1"

    MDIChild = -1 'True

    ScaleHeight = 6240

    ScaleWidth = 8955

    ShowInTaskbar = 0 'False

    Begin MSComDlg.CommonDialog CommonDialog1

    Left = 8040

    Top = 3840

    _ExtentX = 847

    _ExtentY = 847

    _Version = 393216

    End

    Begin VB.ComboBox Combo1

    Height = 300

    Left = 7080

    TabIndex = 10

    Top = 480

    Width = 1335

   End

    Begin VB.CommandButton Command3

    Caption = "关闭"

    Height = 375

    Left = 5280

    TabIndex = 8

    Top = 5640

    Width = 1095

   End

    Begin VB.CommandButton Command2

    Caption = "保存"

    Height = 375

    Left = 2520

    TabIndex = 7

    Top = 5640

    Width = 1095

   End

    Begin VB.CommandButton Command1

    Caption = "浏览"

    Height = 255

    Left = 8040

    TabIndex = 6

    Top = 4800

    Width = 735

   End

    Begin VB.TextBox Text2

    Height = 375

    Left = 1200

    TabIndex = 5

    Top = 4800

    Width = 6375

   End

    Begin RichTextLib.RichTextBox RichTextBox1

    Height = 3615

    Left = 1200

    TabIndex = 3

    Top = 960

    Width = 6375

    _ExtentX = 11245

    _ExtentY = 6376

    _Version = 393217

    Enabled = -1 'True

    TextRTF = $"finput.frx":0000

   End

    Begin VB.TextBox Text1

    Height = 375

    Left = 1200

    TabIndex = 2

    Top = 443

    Width = 4695

   End

    Begin VB.Label Label4

    Caption = "类别"

    Height = 255

    Left = 6240

    TabIndex = 9

    Top = 480

    Width = 615

   End

    Begin VB.Label Label3

    Caption = "图片"

    Height = 255

    Left = 480

    TabIndex = 4

    Top = 4800

    Width = 495

   End

    Begin VB.Label Label2

    Caption = "内容"

    Height = 255

    Left = 480

    TabIndex = 1

    Top = 960

    Width = 495

   End

    Begin VB.Label Label1

    Caption = "标题"

    Height = 255

    Left = 480

    TabIndex = 0

     Top = 503

    Width = 495

   End

   End

    Attribute VB_Name = "finput"

    Attribute VB_GlobalNameSpace = False

    Attribute VB_Creatable = False

    Attribute VB_PredeclaredId = True

    Attribute VB_Exposed = False

    Option Explicit



    Private Sub Command1_Click()

    CommonDialog1.DefaultExt = App.Path

    CommonDialog1.Filter = "Pictures (*.bmp;*.jpg;*.gif) *.bmp;*.jpg;*.gif" '注意要加引号

    CommonDialog1.ShowOpen

    Text2.Text = CommonDialog1.FileName

   End Sub



    '保存文档的标题,和文档的内容,以及相应的图片

    Private Sub Command2_Click()

    '判断是否所写的文档是否已经存在数据库了,如果没有,则保存,否则

    '不能保存(利用一个"临时rs"查询标题)

    Dim subject, sql As String

    Dim temp_photo As Stream

    Dim rs As New ADODB.Recordset

    Dim rs1 As New ADODB.Recordset '定义rs1得到类别的id

    Dim class_id As Integer '定义得到类别的ID号

    subject = Trim(Text1.Text) '获得标题

    sql = "select * from paper where subject='" + subject + "'"

    '开始查询

    rs.Open sql, cn, adOpenDynamic, adLockPessimistic

    '判断标题是否存在



    If rs.EOF Then '文档不存在,开始保存

    Dim tempdate As Date '临时时间变量

    tempdate = Date





    rs.AddNew



    '得到类别的ID

    sql = "select cl_number,class from class where class='" + Combo1.Text + "'"

    rs1.Open sql, cn, adOpenDynamic, adLockPessimistic

    rs("class") = rs1("cl_number")

    rs1.Close '关闭rs1



    rs("subject") = subject

    rs("content") = RichTextBox1.Text

    If Trim(Text2.Text) <> "" Then '假如有图片,开始得到图片文件

    Dim image_data() As Byte '定义图片保存的变量



    Open Trim(Text2.Text) For Binary As #1

    ReDim image_data(LOF(1) - 1)

    Get #1, , image_data()

    rs("photo").AppendChunk image_data()

    End If



    rs("inputtime") = tempdate

    rs("modifytime") = tempdate

    rs.Update '可能出现保存不成功的现象,所以要考虑可能会出现错误

    MsgBox ("保存成功!") '保存成功

    Text1.Text = ""

    RichTextBox1.Text = ""

    Text2.Text = "" '此处清空选择图片的框



    Else '存在,不能保存,显示错误信息

    MsgBox ("文档已经存在,不能保存,请修改!")

   End If



    rs.Close '关闭结果集

   End Sub



    Private Sub Command3_Click()

    Unload Me

    End Sub



    Private Sub Form_Load()

    Me.Left = 0

    Me.Top = 0

    fmain.Width = Me.Width + 340

    fmain.Height = Me.Height + 1550

    '显示文档的类别

    Dim rs As New ADODB.Recordset

    Dim sql As String

    sql = "select * from class"

    rs.Open sql, cn, 1, 1

    Do While Not rs.EOF '类别不空,则添加进去,对应类别的number为索引

    Combo1.AddItem rs("class")

    rs.MoveNext

   Loop

    If rs.RecordCount <> 0 Then '只有查询结果集不为空时,才能设定显示第一项,利用纪录总数不为0判定

    Combo1.ListIndex = 0 '不能用not rs.eof判定,因为现在cursor已经到了最后

   End If

    rs.Close

   End Sub

   当然,在上面这段代码中,还用到了另一个表(表名为class),字段如下:







   字段名 类型 意义

   class 文本 文档类别的名称

   cl_number 数字 类别的编号


 
 

上一篇:在VB中实现移动没有标题栏的窗口  下一篇:文本框的使用说明与技巧