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

📄 form1.frm

📁 将数据库文件转换为文本文件(vb) 将数据库文件转换为文本文件(vb)
💻 FRM
字号:
VERSION 5.00
Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "RICHTX32.OCX"
Object = "{00028C01-0000-0000-0000-000000000046}#1.0#0"; "DBGRID32.OCX"
Begin VB.Form Form1 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "将数据库文件转换为文本文件"
   ClientHeight    =   5370
   ClientLeft      =   1125
   ClientTop       =   1500
   ClientWidth     =   7365
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   PaletteMode     =   1  'UseZOrder
   ScaleHeight     =   5370
   ScaleWidth      =   7365
   ShowInTaskbar   =   0   'False
   StartUpPosition =   1  '所有者中心
   Begin VB.TextBox text1 
      Height          =   285
      Left            =   1725
      TabIndex        =   5
      Top             =   255
      Width           =   4980
   End
   Begin VB.TextBox text2 
      Height          =   285
      Left            =   1725
      TabIndex        =   4
      Top             =   3315
      Width           =   4980
   End
   Begin VB.CommandButton Command2 
      Caption         =   "退出"
      Height          =   465
      Left            =   6270
      TabIndex        =   3
      Top             =   1980
      Width           =   810
   End
   Begin VB.Data Data1 
      Caption         =   "Data1"
      Connect         =   "Access"
      DatabaseName    =   "E:\编程技巧\xcx\数据库\1数据库转为文本\db1.mdb"
      DefaultCursorType=   0  '缺省游标
      DefaultType     =   2  '使用 ODBC
      Exclusive       =   0   'False
      Height          =   345
      Left            =   5430
      Options         =   0
      ReadOnly        =   0   'False
      RecordsetType   =   1  'Dynaset
      RecordSource    =   "sy"
      Top             =   5250
      Visible         =   0   'False
      Width           =   1605
   End
   Begin MSDBGrid.DBGrid DBGrid1 
      Bindings        =   "Form1.frx":0000
      Height          =   2355
      Left            =   270
      OleObjectBlob   =   "Form1.frx":0014
      TabIndex        =   2
      Top             =   780
      Width           =   5805
   End
   Begin RichTextLib.RichTextBox RichText1 
      Height          =   1530
      Left            =   270
      TabIndex        =   1
      Top             =   3705
      Width           =   6795
      _ExtentX        =   11986
      _ExtentY        =   2699
      _Version        =   393217
      ScrollBars      =   3
      TextRTF         =   $"Form1.frx":09B6
   End
   Begin VB.CommandButton command1 
      Caption         =   "转换"
      Height          =   465
      Left            =   6270
      TabIndex        =   0
      Top             =   1380
      Width           =   810
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "数据库路径"
      Height          =   180
      Index           =   0
      Left            =   240
      TabIndex        =   7
      Top             =   285
      Width           =   960
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "输出文本文件路径"
      Height          =   180
      Index           =   1
      Left            =   240
      TabIndex        =   6
      Top             =   3345
      Width           =   1440
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Form_Load()
    Data1.DatabaseName = App.Path & "\db1.mdb" '绑定数据源
    text1.Text = App.Path & "\db1.mdb"
    text2.Text = App.Path & "\db1.txt"
End Sub
Private Sub Command1_Click() '转换数据库数据为文本文件
Dim fnum As Integer
Dim file_name As String
Dim database_name As String
Dim mydb As Database
Dim myrs As Recordset
Dim numcount As Integer
Dim i As Integer
Dim num As Integer
Dim swidth() As Integer
Dim svalue As String
    fnum = FreeFile
    file_name = text2.Text
    Open file_name For Output As fnum
    Set mydb = OpenDatabase(text1.Text) '打开数据库
    Set myrs = mydb.OpenRecordset("SELECT * FROM sy") '打开记录集
    numcount = myrs.Fields.Count
    ReDim swidth(0 To numcount - 1)
    For i = 0 To numcount - 1
        swidth(i) = myrs.Fields(i).Size
        If swidth(i) < Len(myrs.Fields(i).Name) Then
            swidth(i) = Len(myrs.Fields(i).Name)
        End If
        swidth(i) = swidth(i) + 1
        Print #fnum, myrs.Fields(i).Name; '数据写入文本
        Print #fnum, Space$(swidth(i) - Len(myrs.Fields(i).Name))   '写入空格
    Next i
    Print #fnum, ""                                          '写入空行
    Do While Not myrs.EOF
        num = num + 1
        For i = 0 To numcount - 1
            svalue = myrs.Fields(i).Value
            Print #fnum, svalue & Space$(swidth(i) - Len(svalue));
        Next i
        Print #fnum, ""
        myrs.MoveNext                                    '下一条记录
    Loop
    myrs.Close '关闭记录集
    mydb.Close                                            '关闭数据库
    Close fnum
   RichText1.FileName = text2.Text
End Sub
Private Sub Command2_Click()
   End
End Sub

⌨️ 快捷键说明

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