如何将.txt文件读入数据库

pandom 2003-10-23 05:23:04
例如:
SERVICES :

SUBGRP : 1
SUBSIG : CBSET
PSWCAT : 1
NBRIDFCD : CGLIP
CGLIROWR
SUBCTRL : AC24HOUR CGLIR CFWDU
COL : NORMSUB
我怎样把SUBGRP读入数据库的列A中,应该有函数读字符的吧!
...全文
151 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xixigongzhu 2003-10-27
  • 打赏
  • 举报
回复
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
smartcreater01 2003-10-25
  • 打赏
  • 举报
回复
ReadLine + Array
yanshudm 2003-10-25
  • 打赏
  • 举报
回复
楼上,您的程序能不能注释一下
xixigongzhu 2003-10-24
  • 打赏
  • 举报
回复
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
airon2002 2003-10-24
  • 打赏
  • 举报
回复
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
csmmn3 2003-10-23
  • 打赏
  • 举报
回复
split函数可以吧
lzg530 2003-10-23
  • 打赏
  • 举报
回复
帮你顶
orcale 2003-10-23
  • 打赏
  • 举报
回复
就是字符串處理吧
你定義兩個數組你一個處理字段,一個處理值.後面合併就可以了
一行一行讀你txt文件就可以實現了

menuvb 2003-10-23
  • 打赏
  • 举报
回复
你将txt读取后只要将它增加到数据库中就行了。
menuvb 2003-10-23
  • 打赏
  • 举报
回复
举一下。
imports system.io

读取.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
;再下面不写了。你可以在论坛上找一下,很多。
NoReady 2003-10-23
  • 打赏
  • 举报
回复
用SteamReader读入程序,然后再插入数据库
rock29 2003-10-23
  • 打赏
  • 举报
回复
用流(stream)

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧