主题:vb新手求助!关于如何将文本存入到数据库的问题
新手向各位高手请教。
下面是一段将文本文件存入数据库的源码,有一段没看懂,请高手赐教!!!
[color=000000][color=000000][color=FFFFFF][color=008000][color=800000]
'计算文件有多行,设置显示条
fileHand = FreeFile()
Open FileName For Input As fileHand
j = 0
Do While Not EOF(fileHand)
myCHAR = " "
Do While Asc(myCHAR) <> 10
myCHAR = Input(1, #fileHand)
Loop
j = j + 1
Loop
'Do While Not EOF(fileHand)
' Line Input #fileHand, temptxt
'j = j + 1
'Loop
cpbar.Visible = True
cpbar.Max = j
cpbar.Min = 0
cpbar.Value = 0
'为避免频繁打开文件,将文件指针复位
Seek #fileHand, 1
'--------读入记录--------
Do While Not EOF(fileHand)
'赋初值
myCHAR = " " '赋“空格”给myCHAR
temptxt = ""
mySTR = ""
'清空数组
For j = 0 To 10
unit(j) = ""
Next
Do While Asc(myCHAR) <> 10 '如果读取的字符不是回车符,则继续
myCHAR = Input(1, #fileHand)
temptxt = temptxt & myCHAR '将一行数据赋给temptxt
Loop
temptxt = LTrim(temptxt)
j = 0 'J在此处为计数器
cpbar.Value = cpbar.Value + 1
Do While temptxt <> ""
startpos = InStr(1, LTrim(temptxt), " ") '载去字符串前后空格后,将第一个空格前的数字赋予startpos
If startpos <> 0 Then
strlen = startpos - 1 '去掉“ ”标志位
unit(j) = LTrim(Mid(temptxt, 1, strlen))
temptxt = LTrim(Mid(temptxt, startpos + 1))
Else
unit(j) = LTrim(Mid(temptxt, 1))
temptxt = ""
End If
j = j + 1
Loop
If Is_Num(unit(0)) = False Then
'取得机构号?日期?货币符号
If Mid(unit(0), 1, 4) = "机构号:" Then
myJG = Mid(unit(0), 5)
myDay = Format(unit(1), "yyyy-mm-dd")
myCUR = Mid(unit(2), 5, 2)
myClid = Mid(unit(4), 1, 7)
myAccounts = Mid(unit(4), 1, 14)
End If[/color][/color][/color][/color][/color]
上面这段源码是什么意思?另外,unit(0),unit(1),unit(2),unit(3),unit(4)都代表什么?
下面是整段源码。
'@@@@@@@@@@@@@@@@@@
'@ 数据存储函数 @
'@@@@@@@@@@@@@@@@@@
Private Function Receive_Data(FileName As String) As Boolean
Dim fileHand As Integer
Dim j As Variant
Dim temptxt As String
Dim myCHAR As String
Dim mySTR As String
Dim myAcc As String, myBM As String
Dim myDB As String, myCR As String
Dim startpos, strlen As Integer
Dim mySQL As String
Dim rec As ADODB.Recordset '用于检查科目是否正确
Dim myREC, myREC00 As ADODB.Recordset '用于写入的表
'@@@@@@@@@@@@@@@@@@@@@@@@@@
'@ 打开机构核算码余额 @
'@@@@@@@@@@@@@@@@@@@@@@@@@@
Set myREC = New ADODB.Recordset
myREC.CursorLocation = adUseClient ' 将表设为客户端打开
myREC.CursorType = adOpenDynamic
myREC.LockType = adLockBatchOptimistic
myREC.Open "JgHsmB", conn, , adCmdTable
'@@@@@@@@@@@@@@@@@@@@@@@@@@
'@ 打开部门核算码余额 @
'@@@@@@@@@@@@@@@@@@@@@@@@@@
Set myREC00 = New ADODB.Recordset
myREC00.CursorLocation = adUseClient ' 将表设为客户端打开
myREC00.CursorType = adOpenDynamic
myREC00.LockType = adLockBatchOptimistic
myREC00.Open "BmHsmB", conn, , adCmdTable
'计算文件有多行,设置显示条
fileHand = FreeFile()
Open FileName For Input As fileHand
j = 0
Do While Not EOF(fileHand)
myCHAR = " "
Do While Asc(myCHAR) <> 10
myCHAR = Input(1, #fileHand)
Loop
j = j + 1
Loop
'Do While Not EOF(fileHand)
' Line Input #fileHand, temptxt
'j = j + 1
'Loop
cpbar.Visible = True
cpbar.Max = j
cpbar.Min = 0
cpbar.Value = 0
'为避免频繁打开文件,将文件指针复位
Seek #fileHand, 1
'--------读入记录--------
Do While Not EOF(fileHand)
'赋初值
myCHAR = " " '赋“空格”给myCHAR
temptxt = ""
mySTR = ""
'清空数组
For j = 0 To 10
unit(j) = ""
Next
Do While Asc(myCHAR) <> 10 '如果读取的字符不是回车符,则继续
myCHAR = Input(1, #fileHand)
temptxt = temptxt & myCHAR '将一行数据赋给temptxt
Loop
temptxt = LTrim(temptxt)
j = 0 'J在此处为计数器
cpbar.Value = cpbar.Value + 1
Do While temptxt <> ""
startpos = InStr(1, LTrim(temptxt), " ") '载去字符串前后空格后,将第一个空格前的数字赋予startpos
If startpos <> 0 Then
strlen = startpos - 1 '去掉“ ”标志位
unit(j) = LTrim(Mid(temptxt, 1, strlen))
temptxt = LTrim(Mid(temptxt, startpos + 1))
Else
unit(j) = LTrim(Mid(temptxt, 1))
temptxt = ""
End If
j = j + 1
Loop
If Is_Num(unit(0)) = False Then
'取得机构号?日期?货币符号
If Mid(unit(0), 1, 4) = "机构号:" Then
myJG = Mid(unit(0), 5)
myDay = Format(unit(1), "yyyy-mm-dd")
myCUR = Mid(unit(2), 5, 2)
[color=FF00FF]
myClid = Mid(unit(4), 1, 7) '这条语句好像不对
myAccounts = Mid(unit(4), 1, 14) '这条语句好像不对[/color]
End If
'*******按机构取得核算码余额************
If Mid(unit(0), 1, 4) = "核算码:" Then
myAcc = Mid(Trim(unit(0)), 5)
If Check_Rec(myAcc, False) = True Then
'添加新记录
If (myREC.Supports(adAddNew)) Then
myREC.AddNew
myREC("CU") = Trim(myCUR)
myREC("AccID") = myAcc
myREC("DAY") = myDay
myREC("JgID") = myJG
[color=FF0000]
myREC("ClientID") = myClid '该红色源码("ClientID") 和("Accounts") 是我想在数据库中新增加的两列,但在实际运行时无法将下面的文本中的账号存入到数据库中,是空值。
myREC("Accounts") = myAccounts[/color]
'判断读取的数据为负值,则为借方余额
If unit(2) < 0 Then
myREC("DB_BAL") = -unit(2)
myREC("CR_BAL") = 0
Else
myREC("DB_BAL") = 0
myREC("CR_BAL") = unit(2)
End If
myREC.Update
Else
MsgBox "添加机构记录时出错", vbQuestion, "系统警告"
myREC.Close
myREC00.Close
Receive_Data = False
Exit Function
End If
End If
End If
Else
'*******按部门号取得余额********
myAcc = Mid(unit(0), 8, 4)
If Check_Rec(myAcc, True) = True Then
'提取部门号
Set rec = New ADODB.Recordset
mySQL = "select bmid from khh where khhid='" & Mid(unit(0), 1, 7) & "'"
rec.Open mySQL, conn
myBM = rec!bmid
rec.Close
'添加新记录
If (myREC00.Supports(adAddNew)) Then
myREC00.AddNew
myREC00("CU") = Trim(myCUR)
myREC00("AccID") = myAcc
myREC00("DAY") = myDay
myREC00("bmID") = myBM
myREC00("ClientID") = myClid
myREC00("Accounts") = myAccounts
'判断读取的数据为负值,则为借方余额
If unit(3) < 0 Then
myREC00("DB_BAL") = -unit(3)
myREC00("CR_BAL") = 0
Else
myREC00("DB_BAL") = 0
myREC00("CR_BAL") = unit(3)
End If
myREC00.Update
Else
MsgBox "添加部门记录时出错", vbQuestion, "系统警告"
myREC00.Close
myREC.Close
Receive_Data = False
Exit Function
End If
End If
End If
Loop
Close fileHand
myREC.Close
myREC00.Close
Receive_Data = True
End Function
下面是希望存入的文本,上面的源码如何将账号如"86418520148001"和名称如"xxxxxx"存入到数据库中相应的列中呢?请指教!!,谢谢!
机构号:301 2009年03月19日 货币:001
--------------------------------------------------------------------------------
帐号 名称 最后交易日期 余额
--------------------------------------------------------------------------------
86418520148001 xxxxxx 2009-03-19 1,086,116,670.26-
核算码:0148 合计: 1,086,116,670.26-
86417850248001 xxxxxx 2009-03-19 1,081,869,895.57
核算码:0248 合计: 1,081,869,895.57
93011500303001 xxxxxx 2008-05-29 2,685,783.57-
93016100303001 xxxxxx 2008-05-29 7,678,710.43-
下面是一段将文本文件存入数据库的源码,有一段没看懂,请高手赐教!!!
[color=000000][color=000000][color=FFFFFF][color=008000][color=800000]
'计算文件有多行,设置显示条
fileHand = FreeFile()
Open FileName For Input As fileHand
j = 0
Do While Not EOF(fileHand)
myCHAR = " "
Do While Asc(myCHAR) <> 10
myCHAR = Input(1, #fileHand)
Loop
j = j + 1
Loop
'Do While Not EOF(fileHand)
' Line Input #fileHand, temptxt
'j = j + 1
'Loop
cpbar.Visible = True
cpbar.Max = j
cpbar.Min = 0
cpbar.Value = 0
'为避免频繁打开文件,将文件指针复位
Seek #fileHand, 1
'--------读入记录--------
Do While Not EOF(fileHand)
'赋初值
myCHAR = " " '赋“空格”给myCHAR
temptxt = ""
mySTR = ""
'清空数组
For j = 0 To 10
unit(j) = ""
Next
Do While Asc(myCHAR) <> 10 '如果读取的字符不是回车符,则继续
myCHAR = Input(1, #fileHand)
temptxt = temptxt & myCHAR '将一行数据赋给temptxt
Loop
temptxt = LTrim(temptxt)
j = 0 'J在此处为计数器
cpbar.Value = cpbar.Value + 1
Do While temptxt <> ""
startpos = InStr(1, LTrim(temptxt), " ") '载去字符串前后空格后,将第一个空格前的数字赋予startpos
If startpos <> 0 Then
strlen = startpos - 1 '去掉“ ”标志位
unit(j) = LTrim(Mid(temptxt, 1, strlen))
temptxt = LTrim(Mid(temptxt, startpos + 1))
Else
unit(j) = LTrim(Mid(temptxt, 1))
temptxt = ""
End If
j = j + 1
Loop
If Is_Num(unit(0)) = False Then
'取得机构号?日期?货币符号
If Mid(unit(0), 1, 4) = "机构号:" Then
myJG = Mid(unit(0), 5)
myDay = Format(unit(1), "yyyy-mm-dd")
myCUR = Mid(unit(2), 5, 2)
myClid = Mid(unit(4), 1, 7)
myAccounts = Mid(unit(4), 1, 14)
End If[/color][/color][/color][/color][/color]
上面这段源码是什么意思?另外,unit(0),unit(1),unit(2),unit(3),unit(4)都代表什么?
下面是整段源码。
'@@@@@@@@@@@@@@@@@@
'@ 数据存储函数 @
'@@@@@@@@@@@@@@@@@@
Private Function Receive_Data(FileName As String) As Boolean
Dim fileHand As Integer
Dim j As Variant
Dim temptxt As String
Dim myCHAR As String
Dim mySTR As String
Dim myAcc As String, myBM As String
Dim myDB As String, myCR As String
Dim startpos, strlen As Integer
Dim mySQL As String
Dim rec As ADODB.Recordset '用于检查科目是否正确
Dim myREC, myREC00 As ADODB.Recordset '用于写入的表
'@@@@@@@@@@@@@@@@@@@@@@@@@@
'@ 打开机构核算码余额 @
'@@@@@@@@@@@@@@@@@@@@@@@@@@
Set myREC = New ADODB.Recordset
myREC.CursorLocation = adUseClient ' 将表设为客户端打开
myREC.CursorType = adOpenDynamic
myREC.LockType = adLockBatchOptimistic
myREC.Open "JgHsmB", conn, , adCmdTable
'@@@@@@@@@@@@@@@@@@@@@@@@@@
'@ 打开部门核算码余额 @
'@@@@@@@@@@@@@@@@@@@@@@@@@@
Set myREC00 = New ADODB.Recordset
myREC00.CursorLocation = adUseClient ' 将表设为客户端打开
myREC00.CursorType = adOpenDynamic
myREC00.LockType = adLockBatchOptimistic
myREC00.Open "BmHsmB", conn, , adCmdTable
'计算文件有多行,设置显示条
fileHand = FreeFile()
Open FileName For Input As fileHand
j = 0
Do While Not EOF(fileHand)
myCHAR = " "
Do While Asc(myCHAR) <> 10
myCHAR = Input(1, #fileHand)
Loop
j = j + 1
Loop
'Do While Not EOF(fileHand)
' Line Input #fileHand, temptxt
'j = j + 1
'Loop
cpbar.Visible = True
cpbar.Max = j
cpbar.Min = 0
cpbar.Value = 0
'为避免频繁打开文件,将文件指针复位
Seek #fileHand, 1
'--------读入记录--------
Do While Not EOF(fileHand)
'赋初值
myCHAR = " " '赋“空格”给myCHAR
temptxt = ""
mySTR = ""
'清空数组
For j = 0 To 10
unit(j) = ""
Next
Do While Asc(myCHAR) <> 10 '如果读取的字符不是回车符,则继续
myCHAR = Input(1, #fileHand)
temptxt = temptxt & myCHAR '将一行数据赋给temptxt
Loop
temptxt = LTrim(temptxt)
j = 0 'J在此处为计数器
cpbar.Value = cpbar.Value + 1
Do While temptxt <> ""
startpos = InStr(1, LTrim(temptxt), " ") '载去字符串前后空格后,将第一个空格前的数字赋予startpos
If startpos <> 0 Then
strlen = startpos - 1 '去掉“ ”标志位
unit(j) = LTrim(Mid(temptxt, 1, strlen))
temptxt = LTrim(Mid(temptxt, startpos + 1))
Else
unit(j) = LTrim(Mid(temptxt, 1))
temptxt = ""
End If
j = j + 1
Loop
If Is_Num(unit(0)) = False Then
'取得机构号?日期?货币符号
If Mid(unit(0), 1, 4) = "机构号:" Then
myJG = Mid(unit(0), 5)
myDay = Format(unit(1), "yyyy-mm-dd")
myCUR = Mid(unit(2), 5, 2)
[color=FF00FF]
myClid = Mid(unit(4), 1, 7) '这条语句好像不对
myAccounts = Mid(unit(4), 1, 14) '这条语句好像不对[/color]
End If
'*******按机构取得核算码余额************
If Mid(unit(0), 1, 4) = "核算码:" Then
myAcc = Mid(Trim(unit(0)), 5)
If Check_Rec(myAcc, False) = True Then
'添加新记录
If (myREC.Supports(adAddNew)) Then
myREC.AddNew
myREC("CU") = Trim(myCUR)
myREC("AccID") = myAcc
myREC("DAY") = myDay
myREC("JgID") = myJG
[color=FF0000]
myREC("ClientID") = myClid '该红色源码("ClientID") 和("Accounts") 是我想在数据库中新增加的两列,但在实际运行时无法将下面的文本中的账号存入到数据库中,是空值。
myREC("Accounts") = myAccounts[/color]
'判断读取的数据为负值,则为借方余额
If unit(2) < 0 Then
myREC("DB_BAL") = -unit(2)
myREC("CR_BAL") = 0
Else
myREC("DB_BAL") = 0
myREC("CR_BAL") = unit(2)
End If
myREC.Update
Else
MsgBox "添加机构记录时出错", vbQuestion, "系统警告"
myREC.Close
myREC00.Close
Receive_Data = False
Exit Function
End If
End If
End If
Else
'*******按部门号取得余额********
myAcc = Mid(unit(0), 8, 4)
If Check_Rec(myAcc, True) = True Then
'提取部门号
Set rec = New ADODB.Recordset
mySQL = "select bmid from khh where khhid='" & Mid(unit(0), 1, 7) & "'"
rec.Open mySQL, conn
myBM = rec!bmid
rec.Close
'添加新记录
If (myREC00.Supports(adAddNew)) Then
myREC00.AddNew
myREC00("CU") = Trim(myCUR)
myREC00("AccID") = myAcc
myREC00("DAY") = myDay
myREC00("bmID") = myBM
myREC00("ClientID") = myClid
myREC00("Accounts") = myAccounts
'判断读取的数据为负值,则为借方余额
If unit(3) < 0 Then
myREC00("DB_BAL") = -unit(3)
myREC00("CR_BAL") = 0
Else
myREC00("DB_BAL") = 0
myREC00("CR_BAL") = unit(3)
End If
myREC00.Update
Else
MsgBox "添加部门记录时出错", vbQuestion, "系统警告"
myREC00.Close
myREC.Close
Receive_Data = False
Exit Function
End If
End If
End If
Loop
Close fileHand
myREC.Close
myREC00.Close
Receive_Data = True
End Function
下面是希望存入的文本,上面的源码如何将账号如"86418520148001"和名称如"xxxxxx"存入到数据库中相应的列中呢?请指教!!,谢谢!
机构号:301 2009年03月19日 货币:001
--------------------------------------------------------------------------------
帐号 名称 最后交易日期 余额
--------------------------------------------------------------------------------
86418520148001 xxxxxx 2009-03-19 1,086,116,670.26-
核算码:0148 合计: 1,086,116,670.26-
86417850248001 xxxxxx 2009-03-19 1,081,869,895.57
核算码:0248 合计: 1,081,869,895.57
93011500303001 xxxxxx 2008-05-29 2,685,783.57-
93016100303001 xxxxxx 2008-05-29 7,678,710.43-