How to put a plain text file into SQL SERVER database table

Category: Delphi -> Database Author: hc047 Date: 2001-07-09 14:59:53
 
hc047
2001-07-09 14:59:53
I want a plain text file into SQL SERVER database test table, text file format and test table columns correspond to the rule, and each field separated by spaces! !
ask you how do I achieve? ?
lzf2lj
2001-07-09 15:06:47
incoming mail! !
are not too good, will point! ! That is just learning Delphi when writing :)
adndan
2001-07-09 15:23:52
give you one example
For example, there is a personnel file data archive. txt, which reads as follows:
small Xu Xiao Wu, 21 men and 23 women engineers
assistant
Xiaocai 22 men and 22 women assistant
small MU engineers
want it into the database archive. dbf in, archive. dbf structure is as follows:
name, gender, age, title
how to do it? Now through the use of delphi programming, a good solution to this problem. Delphi provides many powerful, rich character handling functions and procedures, commonly used are:
(1) functionLength (S: String): Integer
/ / returns the length of string
(2 ) functionCopy (S: String; Index, Count: Integer): String
/ / string in a string is given a copy of
(3) functionPos (Substr: String; S: String); Integer
/ / Find a substring in the string position
(4) ProcedureDelete (VarS: String; Index, Count: Inte � ger);
/ / remove from a string substring
provided by Delphi functions and procedures have been prepared on the basis of their three functions to achieve a plain text data into the database functions. As long as Delphi supported databases can be supported.
archive. txt for each row of data into a string of each divided data into a field, the division of each field separator characters, this is a space, it can be,,,;, #, etc. symbol. Specifically
idea is: first string adjustment, and with each character in the string delimiter comparison, the delimiter character is not appended to MyStr string, and finally get the contents of a field. Through a loop, it can be a string of
into several fields.
FunctionRegulate (aString, Sepchar: string): string
/ / remove excess separator, canonical string
FunctionGetSubStr (varsString: string; SepChar: String): String;
/ / get the string a substring
/ / aString because they have to change the parameter values, so it defined using var.
FunctionGetSubStrNum (aString, SepChar: String): Integer;
/ / compute a string to be split into several fields.
parameters: aString is required to split a string, SepChar is a separator.
FunctionRegulateStr (aString: String; Sepchar: String): String;
var
i, Num: Integer;
Flag: Boolean;
MyStr, TempStr: String;
begin
Flag: = False ;/ / carry flag, remove excess separator
Num: = Length (aString) ;/ / calculate the length of the string aString
fori: = 1toNumdo
begin
TempStr: = Copy (aString, i, 1) ;/ / get a character string aString
if TempSt> = SepChar then
begin
MyStr: = MyStr + TempDtr;
Flag: = True;
end
else
if (Flag = True) then
begin
Mystr: = Mystr + TempSrt ;
Flag: = False;
end;
end;
if MyStr [Length (MyStr)]> = SepChar then
MyStr: = MyStr + SepChar;
RegulateSrt: = MyStr;
end;
FunctionGetSubStr (varaString: String, SepChar: Strign): String;
var
Mysrt: String;
StrLen : Integer;
SepCharPso: Integer;
begin
StrLen: = Length (aString);
SepCharPos: = Pos (SepChar, aString) ;/ / calculate separator in substring The position
MyStr: = Copy (aString, 1, SepCharPos-1) ;/ / all the characters before the delimiter string into mystr
Delete (aString, 1, SepCharPos) ;/ / remove separator and the separator before the substring
GetSubStr: = MyStr ;/ / return a field
end;
FunctionTforml. GetSubStrNum (aString: String; SepChar: String): Integer;
var
i: Integer;
StrLen: Integer;
Num: Integer;
begin
StrLen: = Length (aString);
Num: = 0;
for i: = 1 to StrLen do
if Copy (aString, i, 1) = SepChar ; then
Num: = Num +1;
GetSubSrtNum: = Num;
end:
With the above three functions are now about a specific application:
1. First create a form Forml, adding a RichEditl (or Menol), a button Buttonl and a Tablel, set Tablel properties:
Tablell. DataBase = 'c: Archivs'
Tablell. TableName = 'Archive. dbf '
2. Were added to the following procedure:
ConstSpace: =''
ProcedureTForml. FormCreate (Sender: Tobject);
begin
RichRditl. Lines. LoadFromFile ('Archive.txt');
end;
ProcedureTForml. Button1Click (Sender: T object);
var
i, j: Integer;
MyLine: String;
begin
with Tablel do
begin
Open;
for i: = 0 to RichEditl. Lines. Count-1 do
begin
MyLine: = RegulateStr (Richeditl.Lines [i], Space);
for j: = 1 to Num To ken ( MyLIne, Space) do
begin
Append;
fileds [j-1]. aString: = GetSubSrt (MyLine, Space));
post;
end;
end;
end;
end;
xwyl123456
2001-07-09 15:34:39
I made two processes
me to Tab between fields for the sub-grid

database to a txt
procedure TForm_main.DBToTxt (SourceTable: TTABLE; Txtname: TFileName);
var
tmptxt: tstringlist;
i: integer;
TmpStr: string;
begin
try
tmptxt: = tstringlist.create;
with Sourcetable do begin
while not eof do begin
tmpstr: ='';
for i: = 0 to fieldcount-1 do
; tmpstr: = tmpStr + fields [i]. AsString + # 9; / / # 9 is tab
; tmptxt.add (tmpstr);
next;
end;
end;
tmptxt.add (# 26); / / # 26 is end
tmptxt.savetofile (txtname);
finally
tmptxt.Free;
end;
end;

txt to database
procedure TForm_main.TxtToDB (SourceTable: TTable; Txtname: TFileName);
var
F1: Textfile;
i, j: integer;
TmpStr, TmpTag, templineno: string;
ch: char;
begin
try
AssignFile (F1, txtname);
reset (f1);
read (f1, ch);
while ch <> # 26 do ; begin
if not SourceTable.Active then SourceTable.Open;
SourceTable. Append;
for i: = 0 to SourceTable.FieldCount-1 do begin
TmpStr: ='';
while (ch <> # 9) do begin / / # 9 is tab
if ch <> # 13 then ; begin / / # 13 is enter
if ch < ;> # 26 then begin / / # 26 is end
Tmpstr: = TmpStr + ch;
read (F1, ch );
end else begin
; closefile (f1);
/ / showmessage ('ok');
; exit;
end;
; end else
read (f1, ch);
; end;
read (F1, ch);

SourceTable.Fields [i]. AsString: = trim (TmpStr);
;
end ;/ / end of (for i: = 0 to SourceTable.FieldCount-1 do)
end ;/ / end of (if not eof (f1) then
except
closefile (f1);
MessageDlg ('received data failed!', mtError, [mbOk], 0);
end;
end;
canrenren
2001-07-09 15:46:39
bcp test in test.txt-c-t-Uname-Ppass-Sserver
Jonzq
2001-07-09 15:52:03
I have done before, is to use stored procedures, but the system is too heavy!
weizhengmin
2001-07-09 16:00:24
My teacher did say should I use stored procedures, but I am not very clear how to deal with!!!
xunyingit
2001-07-09 16:15:48
manual step by step to no good?
aguanga
2001-07-09 16:22:25
vickly Hello, I want to know that you can not help me to do about yourselves?? I have urgently!!

ujs Hello, I really too much food, I could not even enterprise manager did not find it!!: (
oyantiano
2001-07-09 16:41:43
how do you want me to help How about you?
procedures to which you want me to send you?
wx1989fly
2001-07-09 16:48:48
to fuction (I come to the Northeast that ga)
What are you doing? really admire
w252905817
2001-07-09 17:08:37
fuction Thank you for your example, I know that this example
I am now debug this program because this case seems not perfect!!
jenny1889
2001-07-09 17:15:04
vickly,
if you can, then I certainly hope you sent me!!

My mailbox [email protected]
lige7252
2001-07-09 17:31:23
vickly embarrassed mail above me wrong, it should be [email protected]m: P
a5287151
2001-07-09 17:46:57
agree newwen , use bcp package
tjxclh
2001-07-09 17:55:45
vickly:
Thank you for your mail, I have received, thank you very much!!
wenzhixing
2001-07-09 18:13:03
Please send me a copy please? menglica @ 163.com, thank you!
zmlmfdz
2001-07-09 18:16:16
My solution: The file on the server, aware of the location path and file name. delphi support
with query1 do
begin
close;
sql.clear;
sql.add ('BULK INSERT ; au .. mengzi FROM'' d: \ tb \ mengzi.txt'' ');
execsql;
end;
mengzi by sqlserver data import function of the text file into the database, then the data is removed become empty table
ldx1107
2001-07-09 18:24:19
mengli1:
where the data will be deleted ah???
A972715
2001-07-09 18:36:41
few words with DTS get on ah

you point -> point program -> point microsoft sql server-> Point Enterprise Manager ->

Tools - > data into -> click Next -> Select the data source where the text file - "dot next

done directly on OK; hungry ah

Page 1 of 2    «  »