将记事本文件保存到数据库中(Microsoft Access)

时间:2009-06-20 12:56:00

标签: vb6

如何将记事本文件导入VB 6.0,然后将内容保存到数据库(Microsoft Access数据库)

4 个答案:

答案 0 :(得分:1)

基本步骤(适用于任何语言/数据库):

  1. 阅读文件

  2. 使用连接字符串

  3. 连接到数据库
  4. 插入表格

  5. 有关具体信息和代码,请参阅Connecting Access 2003 with Visual Basic 6

答案 1 :(得分:0)

我会在这里阅读这些行,并猜测你正在引用文本文件中保存的某种类型的数据。

如果是这种情况,那么找到的Jet(或ODBC)连接字符串here(以及schema.ini文件)可能正是您所寻找的。

然后,您可以使用ADODB记录集从文本文件中选择数据并将其插入Access数据库。

答案 2 :(得分:0)

好的,我最近这么做了。因此,假设您已将引用设置为SCRRUN.DLL,则第一个函数将读取该文件(并将处理ANSI和Unicode):

Function ReadFileE(Filename As Variant) As String
    Dim oStream As scripting.TextStream
    Dim sData As String

    Set oFSO = New scripting.FileSystemObject
    sData = vbNullString
    On Error Resume Next
    Set oStream = oFSO.OpenTextFile(Filename, ForReading, False, TristateMixed)
    If Err.Number = 0 Then
        sData = oStream.ReadAll
        oStream.Close
    Else
        sData = vbNullString
        End If
        On Error GoTo 0
        ReadFileE = sData
End Function

我用以打开数据库并查询它的以下函数:

Function OpenMDB(sFile As Variant) As Variant  ' ADODB.Connection
    Dim nErr As Long
    Dim sErr As String
    Dim oConnection As Object 'ADODB.Connection
    Set oConnection = CreateObject("ADODB.Connection")
    On Error Resume Next
    oConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sFile
    nErr = Err.Number
    sErr = Err.Description
    On Error GoTo 0
    If nErr <> 0 Then
    Err.Raise OPENDATABASE_E_NOTFOUND, , sErr
    End If
    Set OpenMDB = oConnection
End Function

Function QueryMDB(ByRef oDB As Variant, sQuery As Variant, Optional bCmdText As Boolean = False) As Variant ''ADODB.Connection ADODB.Recordset
    Const adOpenForwardOnly  As Long = 0
    Const adOpenStatic As Long = 3
    Const adOpenDynamic As Long = 2
    Const adOpenKeyset As Long = 1
    Const adLockOptimistic As Long = 3
    Const adCmdText As Long = 1
    Dim oRecordSet As Object 'ADODB.Recordset
    Set oRecordSet = CreateObject("ADODB.RecordSet")
    oRecordSet.Open sQuery, oDB, adOpenKeyset, adLockOptimistic
    Set QueryMDB = oRecordSet
End Function

现在有一些示例代码:

Dim sText As String
sText = ReadFileE("c:\windows\system32\SQLSRDME.TXT")
Dim db As Variant
Dim rs as Variant
set db = OpenMDB("c:\users\bruce\documents\database.mdb")
set rs = QueryMDB( db, "SELECT * FROM Table1" )
rs.Find "Field1='SQLSRDME.TXT'"
If Not rs.EOF Then
    rs("Field2") = sText
    rs.Update
End If
rs.Close
db.Close

最后一点是从我的头顶打字,所以它可能有洞。另一个代码是每天使用的Class文件。

此外,类文件代码在COM对象中公开,因此Variant声明占优势。

HTH。

答案 3 :(得分:-2)

您可能尝试使用Microsoft Excel加载记事本文件,然后尝试从那里将其加载到Microsoft Acces中。