现有一些文本文件,里面的内容都是10003`0157`JF7013528042`2102300535N078000567``028892R078000304`这样格式的若干条数据,我如何用vb把数据导入到后台sql数据库中,最好能批量导入?我不熟悉sql数据库的导入导出操作,麻烦各位能指点一下。最好能把原理讲下。谢谢了!!
我分两步走,先将文本文件导入到grid中,然后再上传到数据库。但是我测试下列代码来将文本文件导入时总是出错,不能成功导入,代码如下:Private Sub Command1_Click()
On Error GoTo err
Text1.Text = ""
Dim a() As Integer
Dim i As Integer
Dim j As Integer
Dim n As String
Dim line As String
With CommonDialog1
.Filter = "全部文件(*.*)|*.*|文本文件(*.txt)|*.txt"
.FilterIndex = 2
.CancelError = True
.InitDir = App.Path
.ShowOpen
End With
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Input As #1
Do While Not EOF(1)
'Line Input #1, line
Line Input #1, n
Text1.Text = Text1.Text & n & vbCrLf
Loop
Close #1
End If
Dim lineCount As Integer
lineCount = SendMessage(Text1.hwnd, _
EM_GETLINECOUNT, 0&, 0&)
'MsgBox lineCount - 1
Dim x() As String
x = Split(Text1.Text, vbCrLf, -1)
Dim y() As String
y = Split(x(0), "'", -1)
ReDim a(lineCount - 1, UBound(y) + 1) As Integer
Open CommonDialog1.FileName For Input As #1
For i = 1 To lineCount - 1
For j = 1 To UBound(y) + 1
Input #1, a(i, j)
Next
Next
Close #1
MSFlexGrid1.Rows = lineCount - 1
MSFlexGrid1.Cols = UBound(y) + 1
For i = 0 To lineCount - 2
MSFlexGrid1.Row = i
For j = 0 To UBound(y)
MSFlexGrid1.Col = j
MSFlexGrid1.Text = a(i + 1, j + 1)
Next
Next
Exit Sub
err:
MsgBox "错误终止了,请重新选择"
Exit Sub
End Sub
请大家帮忙看下。我的文本文件里面的数据格式是:
10003`0157`JF7013528042`2102112321N078000305``2102120291N078000361`
10003`0157`JF7013528042`2102112321N078000305``2102100053N078000145`
10003`0157`JF7013528042`2102112321N078000305``2102350768N078000540`
10003`0157`JF7013528042`2102112321N078000305``21210109956T78000226`
10003`0157`JF7013528041`2102300535N078000568``028892R078000306`
10003`0157`JF7013528041`2102350768N078000283``2102120289N078000495`
10003`0157`JF7013528041`2102350768N078000283``2102300535N078000568`
10003`0157`JF7013528041`2102350768N078000283``2102120289N078000496`
10003`0157`JF7013528041`2102350768N078000283``0355651078000724`
10003`0157`JF7013528041`2102112321N078000273``2102120291N078000359`
10003`0157`JF7013528041`2102112321N078000273``2102100053N078000146`
10003`0157`JF7013528041`2102112321N078000273``2102350768N078000283`
10003`0157`JF7013528041`2102112321N078000273``21210109956T78000229`
10003`0157`JF7013528040`2102300535N078000570``028892R078000339`
10003`0157`JF7013528040`2102350768N078000284``2102120289N078000558`
10003`0157`JF7013528040`2102350768N078000284``2102300535N