转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> Access >> 正文
如何将文本文件转换为ACCESS数据库         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1048 更新时间:2009/4/22 21:27:53

  本文向你介绍如何不用借助Access,直接在程序中创建一个数据库,然后从标准的ASCII文本文件中读取数据到数据库中。原文是微软知识库中的一篇文章,但当时是针对VB3写的,所以其中的代码有点过时。例如现在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

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

在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


[电脑应用]用C#动态创建Access数据库  [Web开发]asp 在线备份与恢复sqlserver数据库代码详解
[电脑应用]教你如何远程管理MSSQL数据库  [其他]关于数据库优化查询计划的方法总结
[电脑应用]Linux数据库大比拚  [JAVA开发]Java连接各种数据库的实例
[聊天工具]QQ IP数据库Build 0825 纯真版__天极Yesky  [系统软件]利用crontab系统每天定时备份MySQL数据库
[系统软件]备份与恢复Windows2003的AD数据库  [常用软件]活用FlashGet数据库
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台