如何将文本文件转换为ACCESS数据库

数据库
Access数据库是由微软发布的关联式数据库管理系统,ACCESS数据库在很多领域得到了广泛的应用,本文将会为大家带来将文本文件转换为ACCESS数据库的方法。

 

导读:如何不用借助Access数据库,直接在程序中创建一个数据库,然后从标准的ASCII文本文件中读取数据到数据库中?例如现在DAO中已没有Table对象,代之以Recordset对象。下文中就会为大家带来解决方案(代码在VB6中调试通过)。
首先在工程中添加对Microsoft DAO 3.51 Library引用。

在窗体中添加三个命令按钮和两个MSFlexGrid.

按照下表设置和控件的属性:

控件 属性 值

Command1 Caption "建立文本文件并显示在网格中"

Command2 Caption "传输入数据并新建一个数据库"

Command3 Caption "显示新数据库中的数据"

Grid1 Cols 5

Grid1 Rows 35

Grid2 Cols 5

Grid2 Rows 35

将下面的代码添加到窗体的声明部分

Dim nums(30) As Long

Dim names(30) As String * 20

Dim addresses(30) As String * 25

Dim ss_nums(30) As String * 12

Const DB_LONG = 4

Const DB_TEXT = 10

Const DB_LANG_GENERAL = ";LANGID=0x0809;CP=1252;COUNTRY=0"

将下面的代码添加到窗体的Load事件中

Sub Form_Load ()

Show

grid1.ColWidth(1) = 1000 'For Emp ID

grid1.ColWidth(2) = 2000 'For Emp Name

grid1.ColWidth(3) = 3000 'For Emp Addr

grid1.ColWidth(4) = 2000 'For Emp SSN

grid1.Col = 1

grid1.Row = 0

grid1.Text = "Emp ID" 'Header for Emp ID from text file

grid1.Col = 2

grid1.Row = 0

grid1.Text = "Emp Name" 'Header for Emp Name from text file

grid1.Col = 3

grid1.Row = 0

grid1.Text = "Emp Addr" 'Header for Emp Addr from text file

grid1.Col = 4

grid1.Row = 0

#p#

grid1.Text = "Emp SSN" 'Header for Emp SSN from text file

grid2.ColWidth(1) = 1000 'For Emp ID

grid2.ColWidth(2) = 2000 'For Emp Name

grid2.ColWidth(3) = 3000 'For Emp Addr

grid2.ColWidth(4) = 2000 'For Emp SSN

grid2.Col = 1

grid2.Row = 0

grid2.Text = "Employee ID" 'Header for Emp ID from DB

grid2.Col = 2

grid2.Row = 0

grid2.Text = "Employee Name" 'Header for Emp Name from DB

grid2.Col = 3

grid2.Row = 0

grid2.Text = "Employee Addr" 'Header for Emp ID from DB

grid2.Col = 4

grid2.Row = 0

grid2.Text = "Employee SSN" 'Header for Emp Name from DB

End Sub

在Command1_Click事件中加入下面的代码

Sub Command1_Click ()

For i% = 1 To 30

nums(i%) = i%

names(i%) = "John Doe # " + Str$(i%)

addresses(i%) = Str$(i%) + " Mocking Bird Lane"

If i% < 9 Then

'* Enter the following four lines as one, single line:

ss_nums(i%) = Trim$(Str$(i%) + Trim$(Str$(i%))

+ Trim$(Str$(i%)) + "-" + Trim$(Str$(i% + 1))

+ Trim$(Str$(i% + 1)) + "-" + Trim$(Str$(i%))

+ Trim$(Str$(i%)) + Trim$(Str$(i%)) + Trim$(Str$(i%)))

Else

'* Enter the following two lines as one, single line:

ss_nums(i%) = Trim$(Trim$(Str$(999)) + "-" + Trim$(Str$(88))

+ "-" + Trim$(Str$(7777)))

End If

Next i%

Open "Testdata.DAT" For Output As #1

For j% = 1 To 30

Print #1, nums(j%)

Print #1, names(j%)

Print #1, addresses(j%)

Print #1, ss_nums(j%)

Next j%

Close #1

For i% = 1 To 30 'Display results from text file

grid1.Col = 1

grid1.Row = i%

grid1.Text = nums(i%) 'Load Emp IDs

grid1.Col = 2

grid1.Row = i%

grid1.Text = names(i%) 'Load Emp Names

grid1.Col = 3

grid1.Row = i%

grid1.Text = addresses(i%) 'Load Emp Addrs

grid1.Col = 4

grid1.Row = i%

grid1.Text = ss_nums(i%) 'Load Emp SSNs

Next i%

End Sub

#p#

在Command2_Click事件中加入下面的代码

Sub Command2_Click ()

Dim newdb As Database

Dim newtb As Table

Dim newtd As New tabledef

Dim newidx As New Index

Dim field1 As New field 'For Emp nums

 

Dim field2 As New field 'For Emp names

Dim field3 As New field 'For Emp addresses

Dim field4 As New field 'For Emp ss_nums

screen.MousePointer = 11 'Display the time to build

Set newdb = CreateDatabase("NEWDB.MDB", DB_LANG_GENERAL)

newtd.Name = "Emp_Table" '* New table name

field1.Name = "Emp_ID" '* Holds Employee ID nums()

field1.Type = DB_LONG

newtd.Fields.Append field1

field2.Name = "Emp_Name" '* Holds Emp names()

field2.Type = DB_TEXT

field2.Size = 20

newtd.Fields.Append field2

field3.Name = "Emp_Addr" '* Holds Employee addr()

field3.Type = DB_TEXT

field3.Size = 25

newtd.Fields.Append field3

field4.Name = "Emp_SSN" '* Holds emp ss_nums()

field4.Type = DB_TEXT

field4.Size = 12

newtd.Fields.Append field4

newidx.Name = "Emp_ID_IDX" '* You have to have an index

newidx.Fields = "Emp_ID"

newidx.Primary = True

newtd.Indexes.Append newidx

newdb.TableDefs.Append newtd

Set newtb = newdb.OpenTable("Emp_Table")

Open "Testdata.dat" For Input As #1

BeginTrans

Do While Not (EOF(1))

newtb.AddNew

Line Input #1, tmp1$ 'Retrieve empl_id

Line Input #1, tmp2$ 'Retrieve empl_name

Line Input #1, tmp3$ 'Re

到这里我要为大家介绍的将文本文件转换为ACCESS数据库的知识就全部讲完了,希望大家都能够从中收获知识,这些内容对的大家的工作将是有益而无害。

【编辑推荐】

  1. 如何使用Access数据库压缩文件
  2. 计算机等级考试二级ACCESS数据库基本使用方法
  3. Java访问ACCESS数据库的方法
  4. SQL Server数据导入到Access数据库
责任编辑:迎迎 来源: pop222.cn
相关推荐

2020-08-28 18:00:29

UnixDOS格式转换

2010-11-15 15:06:58

ORACLE数据库记录

2021-10-19 07:27:08

Unix Dos转换

2010-04-30 17:38:31

Unix文本

2021-11-29 09:46:11

FileReaderJava开发

2009-08-06 18:33:45

C#处理文本文件

2009-09-02 19:13:08

C#处理文本文件

2009-07-15 16:56:59

Jython类型Java类型

2022-03-10 09:08:43

数据库Mongodb数据库转

2011-07-11 10:42:23

SQL数据库横向数据纵向字段

2010-01-15 10:05:35

VB.NET文件对象

2009-09-02 19:08:03

C#实现读取文本文件

2009-09-04 15:56:35

写入文本文件

2009-08-26 11:53:56

C#打印文本文件

2024-02-19 15:38:08

JsonPython字符串

2020-12-17 08:08:15

CentOS

2017-05-25 15:14:36

2015-06-17 14:28:15

Java查询处理方法

2014-03-11 10:11:33

Linux命令more命令文本文件

2010-02-01 14:26:50

C++读写文本文件
点赞
收藏

51CTO技术栈公众号