因为搞了很久也不行,所以改成这样试。哎!也不行,郁闷呀。
运行一次行,第二次开始就失败了。我调试时说SQL Server不存在或拒绝。
注:解决了另外给100分。
还要注意:是写在dll中的。
function myAddRec(strValue:pchar):integer;
var
ErrStr:pchar;
label myExit;
begin
myAddRec:=0;
ErrStr:=pchar('');
with dm do
begin
try
myConn.Connected:=false;
myConn.ConnectionString := 'Provider=SQLOLEDB.1;Password=' + pub_Pwd + ';Persist Security Info=True;User ID=' + pub_User + ';Initial Catalog=' + pub_DataBase + ';Data Source=' + pub_ServerIP ;
myConn.Connected:=True;
except on E:exception do
ErrStr := pchar('DataBase Connect Error:' + E.message);
end;
if ErrStr <> pchar('') then goto myExit; //发生错误 try
myQuery.Close;
myQuery.SQL.Text:='select top 1 col1' +
' from a ' +
' where 1=0';
myQuery.Open;
myQuery.Append;
myQuery.FieldByName('col1').AsString:='2';
myQuery.Post ;
except
ErrStr := pchar('Err');
end;
if ErrStr <> pchar('') then goto myExit; //发生错误
try
myQuery.Close; //用完必须关闭
except on E : exception do
ErrStr := pchar('ERROR: ' + e.message);
end;
if ErrStr <> pchar('') then goto myExit; //发生错误
myAddRec:=1;
goto myExit ;
end;myExit:
try
dm.myQuery.Close;
except
end;
end;
运行一次行,第二次开始就失败了。我调试时说SQL Server不存在或拒绝。
注:解决了另外给100分。
还要注意:是写在dll中的。
function myAddRec(strValue:pchar):integer;
var
ErrStr:pchar;
label myExit;
begin
myAddRec:=0;
ErrStr:=pchar('');
with dm do
begin
try
myConn.Connected:=false;
myConn.ConnectionString := 'Provider=SQLOLEDB.1;Password=' + pub_Pwd + ';Persist Security Info=True;User ID=' + pub_User + ';Initial Catalog=' + pub_DataBase + ';Data Source=' + pub_ServerIP ;
myConn.Connected:=True;
except on E:exception do
ErrStr := pchar('DataBase Connect Error:' + E.message);
end;
if ErrStr <> pchar('') then goto myExit; //发生错误 try
myQuery.Close;
myQuery.SQL.Text:='select top 1 col1' +
' from a ' +
' where 1=0';
myQuery.Open;
myQuery.Append;
myQuery.FieldByName('col1').AsString:='2';
myQuery.Post ;
except
ErrStr := pchar('Err');
end;
if ErrStr <> pchar('') then goto myExit; //发生错误
try
myQuery.Close; //用完必须关闭
except on E : exception do
ErrStr := pchar('ERROR: ' + e.message);
end;
if ErrStr <> pchar('') then goto myExit; //发生错误
myAddRec:=1;
goto myExit ;
end;myExit:
try
dm.myQuery.Close;
except
end;
end;
myQuery.Close;
myQuery.clear; myQuery.SQL.Text:='select top 1 col1' +
' from a ' +
' where 1=0';
myQuery.Open;
myQuery.Append;
myQuery.FieldByName('col1').AsString:='2';
========
没有这个方法的。SQL.clear就有。但这样没有。
var
ErrStr:pchar;
begin
myAddRec:=0;
ErrStr:=pchar(''); with dm do
begin
try
myConn.Connected:=false;
myConn.ConnectionString := 'Provider=SQLOLEDB.1;Password=' + pub_Pwd + ';Persist Security Info=True;User ID=' + pub_User + ';Initial Catalog=' + pub_DataBase + ';Data Source=' + pub_ServerIP ;
myConn.Connected:=True;
except on E:exception do
begin
ErrStr := pchar('DataBase Connect Error:' + E.message);
myConn.Close;
exit;
end; try
myQuery.Close;
myQuery.SQL.Clear;
myQuery.SQL.Add('select top 1 col1' +
' from a ' +
' where 1=0');
myQuery.Open;
myQuery.Append;
myQuery.FieldByName('col1').AsString:='2';
myQuery.Post ;
except
ErrStr := pchar('Err');
myQuery.Close;
myConn.Close;
end; try
myQuery.Close; //用完必须关闭'
myConn.Close;
except on E : exception do
begin
ErrStr := pchar('ERROR: ' + e.message);
Exit;
end;
end; myAddRec:=1; end;end;
myQuery.Close;
myQuery.SQL.Clear;
myQuery.SQL.Add('');
2、少用GOTO语句
if ErrStr <> pchar('') then goto myExit; //发生错误
改为
if ErrStr <> pchar('') then //发生错误
begin
myQuery.Close;
exit;
end;
我的做法是,由主程序传递连接过来,在DLL里创建ADOQUERY,用完之后释放.
begin前面加上dm=Tdm.create(self);