Function Regulate(aString,Sepchar:string):string //去掉多余的分割符,规范字符串 Function GetSubStr(varsString:string;SepChar:String):String; //得到字符串中一个子串,因要改变参数aString的值,所以将它用var定义。 FunctionGetSubStrNum(aString,SepChar:String):Integer; //计算一个字符串要被分割成几个字段。 参数:aString是所需分割的一个字符串,SepChar是分割符。
Function RegulateStr(aString:String;Sepchar:String):String; var i,Num:Integer; Flag:Boolean; MyStr,TempStr:String; begin Flag:=False;//进行标志,去除多余的分割符 Num:=Length(aString);//计算aString串的长度 for i:=1 to Num do begin TempStr:=Copy(aString,i,1);//取aString串中的一字符 if TempStr <> SepChar then begin MyStr:=MyStr+TempStr; 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;
Function GetSubStr(var aString:String,SepChar:Strign):String; var Mysrt:String; StrLen:Integer; SepCharPso:Integer; begin StrLen:=Length(aString); SepCharPos:=Pos(SepChar,aString);//计算分割符在子串中的位置 MyStr:=Copy(aString,1,SepCharPos-1); //将分割符前所有字符放到mystr串中 Delete(aString,1,SepCharPos);//除去分割符和分割符前的子串 GetSubStr:=MyStr;//返回一个字段 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) = SepCharthen Num:=Num+1; GetSubSrtNum:=Num; end;
2.分别加入以下程序: Const Space= '' ProcedureTForml.FormCreate(Sender:Tobject); begin RichRditl.Lines.LoadFromFile('Archive.txt'); end;
ProcedureTForml.Button1Click(Sender:Tobject); var i,j:Integer; MyLine:String; begin &
您正在看的Delphi教程是:文本数据导入数据库的方法。nbsp; 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 do begin Append; fileds[j-1].aString:=GetSubSrt(MyLine,Space)); post; end; end; end; end;
Query.Close; Query.SQL.Clear; Query.SQL.Add('INSERT INTO myimages(FileName,pic) VALUE(''Image1'',:pic)'); Query.ParamByName('pic').LoadFromFile('D:\Hasd.bmp',ftBlob); Query.ExecSQL;
修改:
Query.Close; Query.SQL.Clear; Query.SQL.Add('UPDATE myimages SET pic=:pic WHERE filename=''Image1'''); Query.ParamByName('pic').LoadFromFile('D:\Hasd2.bmp',ftBlob); Query.ExecSQL;
11楼: alexzhang00 Function txtToSQLServer(txtfilename: string): boolean; var adoCon : TADOConnection; adoQuery : TADOQuery; txtSourceFile : TextFile; begin result := False; if Not FileExists(txtfilename) then exit; try assignfile(txtSourceFile,txtfilename); Reset(txtSourceFile); if Eof(txtSourceFile) then exit; adoCon := TADOConnection.Create; adoQuery := TADOQuery.Create(nil); try With adoCon do begin ConnectionString := 'DRIVER={SQL SERVER};SERVER=ERPSERVER;UID=sa;PWD=;Database=TEST'; Connected := True; end; With adoQuery do begin Connection := adoCon; end; except on E:Exception do showmessage(E.Message); end; while not eof(txtSourceFile) do begin With adoQuery do begin sql.clear; sql.add('insert into t1(kqno) values (' + quotedstr(readln(txtSourceFile) + ')'); ExecSQL; end; end; result := true; finally adoCon.close; adoQuery.free; adoCon.free; end; end;
调用: procedure tform1.button1click(sender:Tobject); beign if txtToSQLServer('test.txt') then showmessage('导入成功') else showmessage('导入失败'); end;
请发表评论