delphi直接调用sql 2000或者2005生成的脚本文件,如果是delphi6\7等早期版本由于不支持utf格式,所以生成是一定要选格式为ansi。或者生成后通过其他文本编辑器转换成gb2312编码。如有问题欢迎到编程论坛programbbs.com交流。
//为delphi过滤无法识别的注释信息。
function ClrNoRun(str: string): string;
var
res: string;
oldn, n: integer;
isZs: Boolean;
begin
oldn := 1;
n := 1;
res := '''';
isZs := false;
{if Pos(''seek_calc_gzl_sp'', str) > 0 then
begin
Clipboard.AsText := str;
end;
Clipboard.AsText := str;}
while (n < Length(str)) do
begin
if not isZs then
begin
n := PosEx(''/*'', str, oldn);
if n > 0 then
begin
res := res + Copy(str, oldn, n - oldn);
oldn := n + 2;
isZs := true;
Continue;
end
else
begin
n := Length(str);
res := res + Copy(str, oldn, n - oldn);
oldn := n + 2;
end;
end
else
begin
n := PosEx(''*/'', str, oldn);
if n > 0 then
begin
isZs := false;
oldn := n + 2;
Continue;
end
else
begin
break;
end;
end;
end;
Result := res;
end;
//执行sql脚本文件 Programbbs.com adq参数为窗体中拖入并配置连接字符串的adoQuery控件名称。
//eg: ExecSQLFile(''c:\111.sql'', adoQuery1)
function Tfrmdm.ExecSQLFile(filename: string; adq: TADOQuery): Boolean;
var
strSql, strNoRun: TStrings;
i, j: integer;
bPass: Boolean;
begin
result := false;
if not FileExists(filename) then
exit;
strSql := TStringList.Create;
strNoRun := TStringList.Create;
try
strSql.LoadFromFile(filename);
strNoRun.Append(''SET ANSI_NULLS'');
strNoRun.Append(''SET QUOTED_IDENTIFIER'');
strNoRun.Append(''SET ANSI_PADDING'');
//strNoRun.Append(''/*'');
//strNoRun.Append(''*/'');
adq.Active := false;
adq.SQL.Clear;
for i := 0 to strSql.Count - 1 do
begin
if Trim(str