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;临时根据你的意思写的,如果有错误的话,请见谅,不过方法是对的
exec master..xp_cmdshell 'bcp test..cc out c:\11.txt -w -T'
不知你所说的是什么意思,是不是把txt文件导入到SQL SERVER中,如果是这样的话:
Query1.edit;
//BlobText在你的表中应是Image类型字段
TBlobField(Query1.fieldbyname('BlobText')).Loadfromfile('d:\aa.txt');
Query1.Post;
上面的語句試了一下﹐有這樣錯誤--更新和重整所需的欄位資訊不足﹐不知為何﹐用JEETLIANG的
一開始可以﹐數據改了就不行了﹐可否詳一點﹐謝了
增加:
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;
No.1:是SQL 语句
SQL.LoadFromFile(....Txt);
No.2:是SQL Server里的内容。
如海天子兄那样。
No.3:是作为一个数据表来用,可以用ADO,改变其属性,将此文本文件当做一个数据表。----------------------------------------------------------
事太多、心太乱
2003100109110
2003100109111
2003100109112
2003100109113
2003100109114
2003100109115
2003100109116
2003100109117
我要把test.txt 导入SQL内的一个表,表名为T1,字段名:kqno char(25) ;服务器为ERPSERVER,数据库名为TEST,
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;临时根据你的意思写的,如果有错误的话,请见谅,不过方法是对的
closefile(txtSourceFile);