Imports System
Imports System.IO
Imports System.Collections
Class ReadData
Protected elements As ArrayList = new ArrayList()
Public Shared Sub Main()
Dim data As ReadData = New ReadData("test.txt")'测试程序
Console.WriteLine(data.Item(0, "SUBGRP"))'取第一组元素的SUBGRP字段值
End Sub
Public Sub New(filename As String)
Dim sr As StreamReader = new StreamReader(filename)'读文件
Dim str As String = sr.ReadLine()'读行
Dim hash As Hashtable = Nothing'存储每组元素的键值对
Dim value As String = Nothing
Dim key As String = Nothing
while Not (str is Nothing)
If (str.StartsWith("SERVICES :"))'每组的开始标志
If Not(hash is Nothing)
elements.Add(hash)'将前一个组加入链表
End If
hash = new Hashtable()'重新开始组
Else
Dim index As Integer = str.IndexOf(":")
If (index < 0)'如果没有':',表明这行是上一个键的值
value += str
If Not(key is Nothing)
hash.Item(key) = value'重新设置值
End If
Else
key = str.Substring(0, index).Trim()'取键
value = str.Substring(index + 1).Trim()取值
hash.Add(key, value)'加入当前组
End If
End If
str = sr.ReadLine()
End While
If Not(hash is Nothing)
elements.Add(hash)'将最后一个组加入链表
End If
End Sub
Public Function Item(index As Integer, key As String) As String'取索引表示的组的某个键的值
Return CType(CType(elements.Item(index), Hashtable).Item(key), String)
End Function
End Class
Imports System
Imports System.IO
Imports System.Collections
Class ReadData
Protected elements As ArrayList = new ArrayList()
Public Shared Sub Main()
Dim data As ReadData = New ReadData("test.txt")
Console.WriteLine(data.Item(0, "SUBGRP"))
End Sub
Public Sub New(filename As String)
Dim sr As StreamReader = new StreamReader(filename)
Dim str As String = sr.ReadLine()
Dim hash As Hashtable = Nothing
Dim value As String = Nothing
Dim key As String = Nothing
while Not (str is Nothing)
If (str.StartsWith("SERVICES :"))
If Not(hash is Nothing)
elements.Add(hash)
End If
hash = new Hashtable()
Else
Dim index As Integer = str.IndexOf(":")
If (index < 0)
value += str
If Not(key is Nothing)
hash.Item(key) = value
End If
Else
key = str.Substring(0, index).Trim()
value = str.Substring(index + 1).Trim()
hash.Add(key, value)
End If
End If
str = sr.ReadLine()
End While
If Not(hash is Nothing)
elements.Add(hash)
End If
End Sub
Public Function Item(index As Integer, key As String) As String
Return CType(CType(elements.Item(index), Hashtable).Item(key), String)
End Function
End Class
Dim N As Integer, ss As String, ts As String
Try
N = FreeFile()
FileOpen(N, "c:\12.txt", OpenMode.Input)
Do While Not EOF(N)
ts = LineInput(N)
ss = ss & ts & vbCrLf
'这里就是你读出的一行txt文本,你自己放在数据库,用 split 和 dataset 支操作。 用 Input(N, ss) 这种方法也可·
Loop
FileClose(N)
Catch es As Exception When Err.Number <> 0
MsgBox("err" & Err.Description)
Exit Try
End Try
TextBox2.Text = ss
读取.txt
dim mystreamreas as new streamreader("文件名",system.text.endcoding.default)'这样可以读取txt里面的中文的
dim s as string
s=mystreamreas.readtoend 从流的当前位置读取未尾
'=======================
读入数据库我就不写了。利用dataset来。
imports system.data.oledb
imports system.data
===================================
dim mytable as datatable
dim adocmd as new oldbdataadapter
dim cmd as oledbcommandbuilder
dim dataset=new dataset
;再下面不写了。你可以在论坛上找一下,很多。