1、subject 文本 文本 现在我们就可以创建finput窗口文件来保存图片。首先我们要连接我们的数据库,代码如下:Dim cnstr As String cnstr = Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False; _ amp; Data Source= amp; App.Path amp;realize.mdb;Jet OLEDB:database cn.Open cnstr cn.CursorLocation = adUseClient 这段代码可以放在form_load事件中,当做一个多窗口的系统时,最好放到
2、一个模块文件中,这样在其它窗口中都可以调用这个cn连接。下面是具体的窗口代码:VERSION 5.00 Object= F9043C88-F6F2-101A-A3C9-08002B2F49FB#1.2#0;comdlg32.ocx3B7C8863-D78F-101B-B9B5-04021C009402#1.2#0richtx32.ocxBegin VB.Form finput BorderStyle = 0 None Caption = 文档输入ClientHeight = 6240 ClientLeft = 0 ClientTop = 0 ClientWidth = 8955 Control
3、Box = 0 False LinkTopic = Form1MDIChild = -1 True ScaleHeight = 6240 ScaleWidth = 8955 ShowInTaskbar = 0 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 Widt
4、h = 1335 End Begin VB.CommandButton Command3 关闭Height = 375 Left = 5280 TabIndex = 8 Top = 5640 Width = 1095 Begin VB.CommandButton Command2 保存Left = 2520 TabIndex = 7 Begin VB.CommandButton Command1 浏览Height = 255 TabIndex = 6 Top = 4800 Width = 735 Begin VB.TextBox Text2 Left = 1200 TabIndex = 5 W
5、idth = 6375 Begin RichTextLib.RichTextBox RichTextBox1 Height = 3615 TabIndex = 3 Top = 960 _ExtentX = 11245 _ExtentY = 6376 _Version = 393217 Enabled = -1 TextRTF = $finput.frx:0000 Begin VB.TextBox Text1 TabIndex = 2 Top = 443 Width = 4695 Begin VB.Label Label4 类别Left = 6240 TabIndex = 9 Width = 6
6、15 Begin VB.Label Label3 图片Left = 480 TabIndex = 4 Width = 495 Begin VB.Label Label2 内容TabIndex = 1 Begin VB.Label Label1 标题TabIndex = 0 Top = 503 Attribute VB_Name = finputAttribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = Fa
7、lse Option Explicit Private Sub Command1_Click() CommonDialog1.DefaultExt = App.Path CommonDialog1.Filter = Pictures (*.bmp;*.jpg;*.gif)*.bmp;*.gif 注意要加引号 CommonDialog1.ShowOpen Text2.Text = CommonDialog1.FileName End Sub 保存文档的标题,和文档的内容,以及相应的图片 Private Sub Command2_Click() 判断是否所写的文档是否已经存在数据库了,如果没有,则
8、保存,否则 不能保存(利用一个临时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, adOpenD
9、ynamic, adLockPessimistic 判断标题是否存在 If rs.EOF Then 文档不存在,开始保存 Dim tempdate As Date 临时时间变量 tempdate = Date rs.AddNew 得到类别的ID select cl_number,class from class where class= + Combo1.Text + rs1.Open sql, cn, adOpenDynamic, adLockPessimistic rs(class) = rs1(cl_number) rs1.Close 关闭rs1 subject) = subject c
10、ontent) = 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() photo).AppendChunk image_data() End If inputtime) = tempdate modifytimers.Update 可能出现保存不成功的现象,所以要考虑可能会出现错误
11、 MsgBox (保存成功!) 保存成功 Text1.Text = RichTextBox1.Text = Text2.Text = 此处清空选择图片的框 Else 存在,不能保存,显示错误信息 文档已经存在,不能保存,请修改!End If rs.Close 关闭结果集 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 显示文档的类别 D
12、im sql As String select * from classrs.Open sql, cn, 1, 1 Do While Not rs.EOF 类别不空,则添加进去,对应类别的number为索引 Combo1.AddItem rs(rs.MoveNext Loop If rs.RecordCount 0 Then 只有查询结果集不为空时,才能设定显示第一项,利用纪录总数不为0判定 Combo1.ListIndex = 0 不能用not rs.eof判定,因为现在cursor已经到了最后 rs.Close 当然,在上面这段代码中,还用到了另一个表(表名为class),字段如下:字段名
13、 类型 意义 class 文本 文档类别的名称 cl_number 数字 类别的编号 上面的代码可以较好的保存我们的文档和图片,我们还需要显示我们的图片和文档,现在我们还要显示我们的图片,我做了一个显示窗口(fshow),现在我假设数据库中有 一条记录,subject为ipx协议简介,里面有一个图片(ipx体系结构),窗口代码如下:VERSION 5.00 Object = 831FDD16-0C5C-11D2-A9FC-0000F8754DA1#2.0#0mscomctl.ocxBegin VB.Form fshow 显示图片ClientHeight = 7125 ClientWidth =
14、 10275 ScaleHeight = 7125 ScaleWidth = 10275 Begin VB.Frame Frame2 Height = 6615 Left = 2880 Top = 240 Width = 7335 Left = 5880 Top = 5880 Width = 1215 Height = 4095 Left = 120 Top = 1200 Width = 6975 _ExtentX = 12303 _ExtentY = 7223 fshow.frxBegin VB.Image Image1 Height = 855 Stretch = -1 Begin VB.
15、Line Line4 X1 = 5520 X2 = 5520 Y1 = 5520 Y2 = 6600 Begin VB.Line Line3 X1 = 0 X2 = 7320 Y1 = 5520 Y2 = 5520 Begin VB.Line Line2 Y1 = 960 Y2 = 960 BackColor = amp;H80000009amp;Height = 615 Width = 7095 Begin VB.Frame Frame1 Height = 6735 Width = 2535 Begin MSComctlLib.TreeView TreeView1 Height = 6375
16、 Width = 2295 _ExtentX = 4048 _ExtentY = 11245 PathSeparator = Style = 7 Appearance = 1 Begin VB.Line Line1 BorderColor = amp;H80000001amp;X1 = 2760 X2 = 2760 Y1 = 120 Y2 = 6960 fshowPrivate Sub Command1_Click() Dim temptop, templeft As Long fmain.Top = (Screen.Height - fmain.Height) / 2 fmain.Left
17、= (Screen.Width - fmain.Width) / 2 显示结果 Dim image_filename As String Dim temp_image() As Byte ipx协议简介rs.Open sql, cn, adOpenDynamic, adLockReadOnly Label1.Caption = rs(temp_image() = rs(image_filename = App.Path + temp. + rs(photo_ext建立临时文件 Open image_filename For Binary As #1 Put #1, , temp_image() Close #1 Image1.Picture = LoadPicture(image_filename) 删除临时文件 Kill image_filename 上面代码只能显示一条记录,而且需要先赋条件,显示图片用的是先建一个临时文件,然后把二进制数据读到这个文件里,同时要赋给正确的扩展名,然后可以显示图片,注意,要及时删除临时文件。总结:这种方法只是保存图片的其中一种,还有其它保存到数据库的方法,希望大家不断的交流其它的保存图片的方法。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1