如何从 Access 数据库中的文本文件(未分隔)导入记录?

发布于 2024-12-14 19:01:20 字数 381 浏览 7 评论 0原文

我只是想找出一个使用 Microsoft.Jet.Oledb Provider 4.0 和 Vb.net 从文本文件到 access 数据库的记录导入程序。这看起来很容易,但我的问题不同。在文本文件中,记录没有分隔。我是新手,所以这对我来说太难了。请给出一些提示来解决这个问题。

Format of the text file:
Field Name & Size
Date[Date,ShortDate]FirstName[20]LastName[20]Sex[1]Age[2]
Records
02062011john……………..little………………M15
…
…
…

我可以在使用流阅读器读取文本文件时以编程方式放置分隔符,然后将整个文件导入数据库吗?任何建议

I am just trying figure out a record import program from text file into access database using Microsoft.Jet.Oledb Provider 4.0 and Vb.net. And it seems easy but my problem is different. In the text file, records are not delimited. I am a newbie so it became too hard for me. Please give some hint to solve this issue.

Format of the text file:
Field Name & Size
Date[Date,ShortDate]FirstName[20]LastName[20]Sex[1]Age[2]
Records
02062011john……………..little………………M15
…
…
…

Can I put delimiter programmatically while reading the text file using stream reader and later simply import the whole file in DB. Any suggestions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜巴黎 2024-12-21 19:01:21

一种选择是使用 TextFieldParser 类:

http:// msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx

Imports Microsoft.VisualBasic.FileIO

Private Sub ReadFixedWidthText()
    Dim theString As String = "John                Little              M15" + vbCrLf + "Jane                Doe                 F30"

    Using rdr As New StringReader(theString)
        Using parser As New TextFieldParser(rdr)
            parser.TextFieldType = FieldType.FixedWidth
            parser.FieldWidths = New Integer() {20, 20, 1, 2}
            parser.TrimWhiteSpace = True

            While Not parser.EndOfData
                Dim fields() As String = parser.ReadFields()

                For i As Integer = 0 To fields.Length - 1
                    Console.WriteLine("Field {0}: {1}", i, fields(i))
                Next
            End While
        End Using
    End Using
End Sub

我在此示例中使用了 StringReader,但您也可以轻松使用StreamReader 或仅在 TextFieldParser 构造函数中使用文件名。

或者,您可以使用 StreamReader 和 String.Substring 方法的组合来获取各个字段。

One option is to use the TextFieldParser class:

http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx

Imports Microsoft.VisualBasic.FileIO

Private Sub ReadFixedWidthText()
    Dim theString As String = "John                Little              M15" + vbCrLf + "Jane                Doe                 F30"

    Using rdr As New StringReader(theString)
        Using parser As New TextFieldParser(rdr)
            parser.TextFieldType = FieldType.FixedWidth
            parser.FieldWidths = New Integer() {20, 20, 1, 2}
            parser.TrimWhiteSpace = True

            While Not parser.EndOfData
                Dim fields() As String = parser.ReadFields()

                For i As Integer = 0 To fields.Length - 1
                    Console.WriteLine("Field {0}: {1}", i, fields(i))
                Next
            End While
        End Using
    End Using
End Sub

I used a StringReader in this example, but you could just as easily use a StreamReader or just use the filename in the TextFieldParser constructor.

Or you could use a combination of the StreamReader and the String.Substring method to get the individual fields.

╰ゝ天使的微笑 2024-12-21 19:01:21

不是用空格分隔的吗?您对文本文件有任何限制吗,例如从第一个字符到第 100 个字符是名字等?如果是,您可以根据这些约束来打破文本。

Is it not space delimited? Do you have any constraints on text file like for example from first character till 100th character is first name etc? If yes you can break the text on the basis of those constraints.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文