|
楼主 |
发表于 2016-4-15 12:19
|
显示全部楼层
前面把数据存入txt文件了,如何从txt文件中取回数据呢,很简单,现在把程序贴在下面,讲解在图中:
如果按以上程序,取回数据如下,各位看图会看到多取回了一行:
原因如下:
修改后的程序如下:
- Sub fromfile11()
- Dim m_Sj As udtGx
- Dim strPathToFile As String
- Dim arr(1 To 1000, 1 To 5)
- Dim myFileNo As Integer
- Dim k As Long
- strPathToFile = "d:\temp\fltab.dat"
- myFileNo = FreeFile
- Open strPathToFile For Random As myFileNo Len = Len(m_Sj)
- With m_Sj
- Do
- Get myFileNo, , m_Sj
- If EOF(1) Then Exit Do ‘在这里判断是否到了文件最后,及时退出,免得多取一行
- k = k + 1
- arr(k, 1) = Trim(.bh)
- arr(k, 2) = Trim(.xh)
- arr(k, 3) = Trim(.bj)
- arr(k, 4) = Trim(.lj)
- arr(k, 5) = Trim(.gx)
- Loop
- Close myFileNo
- End With
- With ThisWorkbook.Worksheets("fltab")
- .Range("g2:q6000").Clear
- .Range("g2").Resize(k, 5) = arr
- End With
- End sub
复制代码 以下程序把文本文件中的记录插入数据库:
- Sub tobase()
- Dim cmd As Object
- Dim mj As udtGx
- Dim strPathToFile As String
- Connect "NorthwindSQL"
- Conn.Open
- strPathToFile = "d:\temp\fltab.dat"
- Set cmd = CreateObject("adodb.command")
- With cmd
- .CommandText = "INSERT INTO fltab VALUES (?,?,?,?,?)"
- Set .activeconnection = Conn
- Open strPathToFile For Random As #1 Len = Len(mj)
- .Parameters.Refresh
- With mj
- Do
- Get #1, , mj
- If EOF(1) Then Exit Do
- cmd.Execute Parameters:=Array(.bh, .xh, .bj, .lj, .gx), _
- Options:=adCmdText + adExecuteNoRecords
- Loop
- End With
- Close #1
- End With
- End Sub
复制代码 这方面的东西还有好多没有讲,讲了就太失之于琐碎了,下一讲说说更新数据库表
|
评分
-
3
查看全部评分
-
|