请教达人:如何在VB中将txt文档中的海量数据转入mdb数据库
\u5982\u4f55\u7528vb\u628atxt\u6587\u4ef6\u7684\u6570\u636e\u5bfc\u5165access\u6570\u636e\u5e93\uff1f\u7f16\u5199\u4e00\u4e2aSchema.ini\u653e\u5728Txt\u6587\u4ef6\u7684\u76ee\u5f55\u4e0b
[customer.txt] //Txt\u6587\u4ef6\u540d
ColNameHeader=True
format=Delimited(",") //\u6307\u5b9a\u5206\u9694\u7b26
MaxScanRows=0
CharacterSet=ANSI
Col1="\u5b57\u6bb51" Char Width 20 //\u6307\u5b9a\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b
Col2="\u5b57\u6bb52" Char Width 20
\u7136\u540e\u5728\u7a0b\u5e8f\u4e2d\u6267\u884cSQL\uff1a
Select * Into customer From [Text;Database=c:\temp].customer.txt
\u9996\u5148\uff0c\u5efa\u7acb\u4e00\u4e2aAccess2000\u6570\u636e\u5e93data.mdb\uff0c\u5728\u5e93\u4e2d\u5efa\u7acb\u4e00\u4e2a\u8868userdata\uff0c\u91cc\u9762\u6709\u201c\u5de5\u53f7\u201d\u3001\u201c\u59d3\u540d\u201d\u3001\u201c\u6027\u522b\u201d\u3001\u201c\u90e8\u95e8\u201d\u56db\u4e2a\u5b57\u6bb5\uff0c\u90fd\u662f\u5b57\u7b26\u578b\u7684\uff0c\u518d\u8f93\u5165\u4e00\u4e9b\u6570\u636e\uff0c\u518d\u5efa\u7acb\u4e00\u4e2a\u7a97\u4f53Form1\uff0c\u5728Form1\u4e2d\uff0c\u653e\u7f6e\u4ee5\u4e0b\u63a7\u4ef6\uff1a
\u63a7\u4ef6\u540d\u79f0 \u5c5e\u6027 \u503c \u8bf4\u660e
Tlabel1 Caption "\u8bf7\u8f93\u5165\u6587\u4ef6\u540d"
TEdit1 Name "" \u7528\u6765\u8f93\u5165\u6587\u4ef6\u540d\uff0c\u5e26\u6269\u5c55\u540d\u7684
TButton1 Caption "\u4fdd\u5b58"
TButton2 Caption "\u9000\u51fa"
ADOTable1 Active
ConnectionString
TableName True
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb;Persist Security Info=False
userdata
\u4e0b\u9762\u662f\u76f8\u5173\u4ee3\u7801:
file://---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant OLEObject;
AnsiString dataword;
if(Trim(Edit1->Text)=="")
{
ShowMessage("\u8bf7\u8f93\u5165\u6587\u4ef6\u540d");
return;
}
String currentPath=GetCurrentDir()+"//"+Trim(Edit1->Text);
OLEObject=CreateOleObject("Word.Basic");
OLEObject.Exec(Procedure("FileNew"));
OLEObject.Exec(Procedure("EndOfDocument"));
ADOTable1->First();
while(ADOTable1->Eof!=True)
{
dataword=ADOTable1->FieldValues["\u5de5\u53f7"]+ADOTable1->FieldValues["\u59d3\u540d"]+ADOTable1->FieldValues["\u6027\u522b"]+ADOTable1->FieldValues["\u90e8\u95e8"];
OLEObject.Exec(Procedure("Insert")<<dataword);
ADOTable1->Next();
}
OLEObject.Exec(Procedure("StartOfDocument"));
OLEObject.Exec(Procedure("FileSaveAs")<<currentPath);
OLEObject.Exec(Procedure("FileClose")<<1);
ShowMessage("\u6587\u4ef6\u5df2\u7ecf\u5199\u5165\uff01\u8c22\u8c22\u4f7f\u7528\uff01");
}
---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
this->Close();
}
---------------------------------------------------------------------------
void __fastcall TForm1::Edit1Click(TObject *Sender)
{
Edit1->Text="";
}
例:
aaa.txt 位于G盘根目录
格式为:
字段名1,字段名2
000,2222
111,2233
库:db1.mdb,表Table1,字段:字段名1,字段名2
代码:
Dim db As Database, tbl As TableDef
Set db = DBEngine.OpenDatabase("g:\db1.mdb") '打开数据库
Set tbl = db.CreateTableDef("temp") '创建一个临时的文本链接表
tbl.Connect = "text;database=g:\" 'TXT文件所在的目录
tbl.SourceTableName = "aaa#txt" 'TXT文件的名称
db.TableDefs.Append tbl '将表加入到数据库中
db.Execute "insert into table1 select * from temp"
db.TableDefs.Delete tbl.Name '删除temp链接表
db.Close: Set db = Nothing '关闭数据对象
If Err = 0 Then
MsgBox "导入成功! "
Else
MsgBox "错误" & Err
Exit Sub
End If
--------------------
以上代码,VB6.0测试通过
首先在程序目录建立fff.mdb数据库,最好你自己建立。然后在数据库建立一个数据表,比如“数据”,在其中添加三个字段:ID、数据1、数据2。
太疲倦了,明天回答你。
绛旓細FileName = App.Path & "\1.txt" '璇锋牴鎹疄闄呮儏鍐典慨鏀 If Dir(FileName) = "" Then End ReDim b(FileLen(FileName))Open FileName For Binary As #1 Get #1, , b Close #1 S = StrConv(b, vbUnicode)S = Replace(S, vbKeyTab, " ")a = Split(S, vbNewLine)Recn = 0 Show...
绛旓細'涓嬮潰鐨勪唬鐮佷粠鏂囨湰妗嗕腑璇诲彇鍐呭锛屽苟涓1.txt姣旇緝锛鎶涓嶉噸澶嶉儴鍒嗚拷鍔犲埌鏂囦欢灏鹃儴 Private Sub Command1_Click()Dim i As Integer Dim r() As String Dim t() As String Dim j As Integer, l As Integer Open "1.txt" For Input As #1 While Not EOF(1)i = i + 1 ReDim Preserve r(1...
绛旓細鐢ㄦ垜鎻愪緵鐨勫嚱鏁板彲浠ュ仛鍒版埅鍙朅瀛楃涓插拰B瀛楃涓蹭腑闂村瓧绗︿覆锛欶unction GetByDiv(ByVal code As String, ByVal divBegin As String, divEnd As String) '鑾峰彇鍒嗛殧绗︽墍澶圭殑鍐呭[瀹屾垚锛岄氳繃娴嬭瘯]Dim lgStart As Long Dim lens As Long Dim lgEnd As Long lens = Len(divBegin)lgStart = InStr(1...
绛旓細Option ExplicitDim i%, n%Private Sub Command1_Click() Dim Str() As String, s1 As String Open "c:\dir.txt" For Input As #1 Open "c:\dir1.txt" For Append As #2 Dim Line As String Do Until EOF(1) Line Input #1, Line If i = 5 * n + 2 T...
绛旓細print #1 , b(a)涓鍙ユ湯灏惧姞鍒嗗彿锛涘嵆锛歱rint #1 , b(a);
绛旓細鎴戞兂瑕佸疄鐜颁互涓嬬殑鍔熻兘:鍦═ext鏂囨湰妗嗕腑杈撳叆涓娈典俊鎭,鐒跺悗鐐瑰嚮鎸夐挳,灏变細鎶婃墍鏈塧ccess鏁版嵁搴撲腑鐩稿叧鐨勪俊鎭互涓鏉′竴鏉$殑鏍煎紡鏄剧ず鍦ㄤ笅鎷夊垪琛ㄤ腑銆傚尶鍚 | 娴忚2734 娆 |涓炬姤 鎴戞湁鏇村ソ鐨勭瓟妗堟帹鑽愪簬2016-01-21 07:08:13 鏈浣崇瓟妗 鍙傝冧唬鐮佸涓:Private Sub Command1_Click() on error goto errhandle Dim Cnn As New...
绛旓細FileName = Dir(SrcPath & "\*.txt")Do While FileName <> ""ReDim BB(FileLen(SrcPath & "\" & FileName) - 1)Open SrcPath & "\" & FileName For Binary As #1 Get #1, , BB Close #1 s = StrConv(BB, vbUnicode)s = Replace(s, ",,", vbNewLine & vbNewLine & " "...
绛旓細'鎸夌収浣犵殑鎰忔濓紝搴旇鏄妸瀛楁瘝鍜屾暟瀛楄嚜鍔ㄦ壘鍒板垎闅旂鍒嗗紑锛屽鏋滀綘纭畾鍒嗛殧绗︽槸"|"骞朵笖鍙湁涓ら儴鍒嗛偅灏卞ソ鍔烇紝split鍑芥暟灏卞彲浠'濡傛灉鏄嚜鍔ㄥ鎵撅紝閭d箞灏辫鐢ㄥ埌Asc鍑芥暟鏉ュ尯鍒嗗瓧姣嶅拰鏁板瓧锛岃繕澶嶆潅浜嗙壍娑夊埌鍒嗛殧绗︽槸鍚﹀娆″嚭鐜扮殑鍒ゆ柇 Private ClickNum As Integer '澹版槑涓涓叏灞鐐瑰嚮鏁扮洰鍙橀噺Private Str As String '...
绛旓細Shell "Explorer.exe" & Chr(34) & 鏂囦欢鍦板潃 & Chr(34),1,0,0 杩欐牱Shell璋冪敤explorer.exe鍙浣犳墍鎸囧悜鐨勬枃浠跺湴鍧鏄纭殑涓斿湪绯荤粺涓湁鍙敤鐨勬枃浠跺叧鑱斿氨鑳借姝g‘鐨勬墦寮銆傚鏋滄槸.exe 锛.bat绛変細鐩存帴琚墽琛屻傚鏋滄槸.txt浼氱洿鎺ョ敤璁颁簨鏈墦寮銆
绛旓細vb.net浠庢枃浠惰矾寰勪腑鑾峰彇鏂囦欢鍚 濡傞,姣斿鎴戠敤鏂囦欢VB.NET鎵撳紑浜嗕竴涓枃浠舵枃浠惰矾寰勬槸 " *:\*\*.* 鈥 (濡"C:\wie\saf\as.txt")鎴戝彧鎯宠鍚庨潰鐨勬枃浠跺悕(as.txt),鎴戞兂瑕佺殑鏄傚悎鍏ㄩ儴璺緞鐨,涓嶅彧鏄拡瀵"C:\wie\saf\as.txt鈥濊繖涓矾寰勭殑,姹傞珮鎵嬩簡!!!