主题:如何把.TXT文件读入到SQL数据库中?
这是VB6里面的语句:
Private Sub CmdData_Click()
Dim Count, Fsum As Integer
Comm1.Filter = "TEXT FILES(*.TXT)|*.TXT"
Comm1.FilterIndex = 2
Comm1.ShowOpen
FileName = Comm1.FileName
If FileName = "" Then Exit Sub
Open FileName For Input As #1
Do While Not EOF(1)
Line Input #1, SumS
Count = Count + 1 '统计文本的行数
Loop
Close #1
Open FileName For Input As #1
Do While Not EOF(1)
Input #1, CARD_NO, CARD_DATE, CARD_CLOCK, CARD_FALG '抓出文本里的数据
CARD_NO = "000000" & CARD_NO '因为CARD_NO抓出来是数字型的,所以在前面加上4个0
txtSQL = "SELECT * FROM CLOCK_CARD"
Set mrc = ExecuteSql_CARD(txtSQL, MsgText)
'读入数据
mrc.AddNew
mrc!CARD_NO = Right(CARD_NO, 8) '写入数据库时,抓右边10个字符
mrc!CARD_NO_NEW = Right(CARD_NO, 10)
mrc!CARD_DATE = Left(CARD_DATE, 4) & "-" & Mid(CARD_DATE, 5, 2) & "-" & Mid(CARD_DATE, 7, 2)
mrc!CARD_TIME = Mid(CARD_DATE, 9, 2) & ":" & Mid(CARD_DATE, 11, 2)
mrc!CARD_CLOCK = Right("00" & CARD_CLOCK, 3)
mrc!CARD_FLAG = CARD_FALG
mrc.Update
Me.Caption = "正在处理...." & Fsum & "/" & (Count - 1)
Fsum = Fsum + 1
Loop
Close #1
Me.Caption = "打卡资料处理"
'把名字和卡号关联起来
txtSQL = "UPDATE CLOCK_CARD INNER JOIN EMPLOYEE ON CLOCK_CARD.CARD_NO_NEW=EMPLOYEE.card_id SET CLOCK_CARD.CARD_ID = EMPLOYEE.EMPL_ID"
Set mrc1 = ExecuteSql_CARD(txtSQL, MsgText)
'把处理好的TXT文件备份起来
Dim FSO As New FileSystemObject, Fill
Set Fill = FSO.GetFile(FileName)
Fill.Move ("C:\CARD\BACKUP\" & Right(Comm1.FileName, 12))
MsgBox "处理完成"
End Sub
我对上面的语句用VB.NET进行了修改,变成这样,可是再下去我就不会了,请高手老师帮我修改好吗,谢谢
Imports System.IO
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class FrmMain
Inherits System.Windows.Forms.Form
Public txtSQL As String
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim strData As String
Dim myStream As Stream
Dim openFileDialog1 As New OpenFileDialog
openFileDialog1.InitialDirectory = "c:\CARD\BACKUP\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = openFileDialog1.OpenFile()
If Not (myStream Is Nothing) Then
' Insert code to read the stream here.
Dim objreader As StreamReader = New StreamReader(myStream)
strData = objreader.ReadToEnd()
objreader.Close()
objreader = Nothing
myStream.Close()
End If
End If
End Sub
End Class
[em1][em1]
Private Sub CmdData_Click()
Dim Count, Fsum As Integer
Comm1.Filter = "TEXT FILES(*.TXT)|*.TXT"
Comm1.FilterIndex = 2
Comm1.ShowOpen
FileName = Comm1.FileName
If FileName = "" Then Exit Sub
Open FileName For Input As #1
Do While Not EOF(1)
Line Input #1, SumS
Count = Count + 1 '统计文本的行数
Loop
Close #1
Open FileName For Input As #1
Do While Not EOF(1)
Input #1, CARD_NO, CARD_DATE, CARD_CLOCK, CARD_FALG '抓出文本里的数据
CARD_NO = "000000" & CARD_NO '因为CARD_NO抓出来是数字型的,所以在前面加上4个0
txtSQL = "SELECT * FROM CLOCK_CARD"
Set mrc = ExecuteSql_CARD(txtSQL, MsgText)
'读入数据
mrc.AddNew
mrc!CARD_NO = Right(CARD_NO, 8) '写入数据库时,抓右边10个字符
mrc!CARD_NO_NEW = Right(CARD_NO, 10)
mrc!CARD_DATE = Left(CARD_DATE, 4) & "-" & Mid(CARD_DATE, 5, 2) & "-" & Mid(CARD_DATE, 7, 2)
mrc!CARD_TIME = Mid(CARD_DATE, 9, 2) & ":" & Mid(CARD_DATE, 11, 2)
mrc!CARD_CLOCK = Right("00" & CARD_CLOCK, 3)
mrc!CARD_FLAG = CARD_FALG
mrc.Update
Me.Caption = "正在处理...." & Fsum & "/" & (Count - 1)
Fsum = Fsum + 1
Loop
Close #1
Me.Caption = "打卡资料处理"
'把名字和卡号关联起来
txtSQL = "UPDATE CLOCK_CARD INNER JOIN EMPLOYEE ON CLOCK_CARD.CARD_NO_NEW=EMPLOYEE.card_id SET CLOCK_CARD.CARD_ID = EMPLOYEE.EMPL_ID"
Set mrc1 = ExecuteSql_CARD(txtSQL, MsgText)
'把处理好的TXT文件备份起来
Dim FSO As New FileSystemObject, Fill
Set Fill = FSO.GetFile(FileName)
Fill.Move ("C:\CARD\BACKUP\" & Right(Comm1.FileName, 12))
MsgBox "处理完成"
End Sub
我对上面的语句用VB.NET进行了修改,变成这样,可是再下去我就不会了,请高手老师帮我修改好吗,谢谢
Imports System.IO
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class FrmMain
Inherits System.Windows.Forms.Form
Public txtSQL As String
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim strData As String
Dim myStream As Stream
Dim openFileDialog1 As New OpenFileDialog
openFileDialog1.InitialDirectory = "c:\CARD\BACKUP\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = openFileDialog1.OpenFile()
If Not (myStream Is Nothing) Then
' Insert code to read the stream here.
Dim objreader As StreamReader = New StreamReader(myStream)
strData = objreader.ReadToEnd()
objreader.Close()
objreader = Nothing
myStream.Close()
End If
End If
End Sub
End Class
[em1][em1]