因为搞了很久也不行,所以改成这样试。哎!也不行,郁闷呀。
运行一次行,第二次开始就失败了。我调试时说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;

解决方案 »

  1.   

    try
           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';
      

  2.   

    wuruichang(迷糊) 
    ========
    没有这个方法的。SQL.clear就有。但这样没有。
      

  3.   

    function myAddRec(strValue:pchar):integer;stdcall;
    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;
      

  4.   

    1、用 
    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;
      

  5.   

    你们都没有实际写过吧。代码在delphi程序中是可以的。但dll就出现这种情况。
      

  6.   

    ADOQuery 需要在DLL内部创建,如果使用主程序传递过来的,会出现你所说的情况.
    我的做法是,由主程序传递连接过来,在DLL里创建ADOQUERY,用完之后释放.
      

  7.   

    太麻烦了。我还是用vc做了,delphi真让人失望
      

  8.   

    在  with dm do
      begin前面加上dm=Tdm.create(self);