//导入数据
function TThreadNet.ExecteDataImport: Boolean;
begin
  CoInitialize(nil);
  Result := False;
  try
    Dts_SQLConnection := TADOConnection.Create(nil);
    Dts_SqlQuery := TADOQuery.Create(nil);
    Dts_SQLConnection.Provider := 'SQLOLEDB';
    Dts_SQLConnection.ConnectionString := 'Provider=SQLOLEDB.1;User ID='+Trim(Form1.IniSqlLoginName)
                                         +';Password='+Trim(Form1.IniSqlLoginPassword)+';Initial Catalog='
                                         +Trim(Form1.IniSqlDataBase)+';Data Source='+Trim(Form1.IniSqlName);
    Dts_SQLConnection.LoginPrompt := False;
    Dts_SQLConnection.CommandTimeout := 0;
    Dts_SQLConnection.ConnectionTimeout := 0;
    Dts_SQLConnection.Open;
    try
      Form1.AddWorkListItem('连接本地数据服务器','连接成功');
      Dts_SqlQuery.Connection := Dts_SQLConnection;
      Dts_SqlQuery.CommandTimeout := 0;
      Dts_SqlQuery.SQL.Clear;
      Dts_SqlQuery.SQL.Add('Exec DataInTrans :InPath');
      Dts_SqlQuery.Parameters.ParamByName('InPath').Value := Form1.IniSqlInDir+'\';
      Form1.NetServerInfo.Strings[2] := '正在导入数据';
      Form1.TcpServer.Socket.Write(Form1.NetServerInfo,True);
      Form1.AddWorkListItem('执行导入数据','请稍候...');
      Dts_SqlQuery.ExecSQL;
      Dts_SqlQuery.Close;
      Dts_SQLConnection.Close;
      Form1.DownFileList.RefreshItems;
      if Form1.DownFileList.Items.Count <> 0 then
      begin
        Form1.AddWorkListItem('校验数据是否导入成功','数据未完全导入,请与管理员联系');
        if Form1.TCPServer.Connected then Form1.TCPServer.Disconnect;
        if Form1.FTPConnect.Connected then Form1.FTPConnect.Disconnect;
        Dts_SqlQuery.Close;
        Dts_SQLConnection.Close;
        Exit;
      end;
      Form1.NetServerInfo.Strings[2] := '导入数据成功';
      Form1.TcpServer.Socket.Write(Form1.NetServerInfo,True);
      Form1.AddWorkListItem('执行数据导入','导入成功');
      Result := True;
    except
      on E:Exception do
      begin
        Application.MessageBox(PChar(E.Message),'Error');
        if Form1.TCPServer.Connected then Form1.TCPServer.Disconnect;
        if Form1.FTPConnect.Connected then Form1.FTPConnect.Disconnect;
        Dts_SqlQuery.Close;
        Dts_SQLConnection.Close;
        Form1.AddWorkListItem('执行数据导入','执行错误,请与管理员联系');
        Exit;
      end;
    end;
  except
    if Form1.TCPServer.Connected then Form1.TCPServer.Disconnect;
    if Form1.FTPConnect.Connected then Form1.FTPConnect.Disconnect;
    Dts_SQLConnection.Close;
    Form1.AddWorkListItem('连接本地数据服务器','连接失败,请与管理员联系');
    Exit;
  end;
  CoUninitialize;
end;//导出数据
function TThreadNet.ExecteDataExprot: Boolean;
begin
  CoInitialize(nil);
  Result := False;
  try
    Dts_SQLConnection := TADOConnection.Create(nil);
    Dts_SqlQuery := TADOQuery.Create(nil);
    Dts_SQLConnection.Provider := 'SQLOLEDB';
    Dts_SQLConnection.ConnectionString := 'Provider=SQLOLEDB.1;User ID='+Trim(Form1.IniSqlLoginName)
                                         +';Password='+Trim(Form1.IniSqlLoginPassword)+';Initial Catalog='
                                         +Trim(Form1.IniSqlDataBase)+';Data Source='+Trim(Form1.IniSqlName);
    Dts_SQLConnection.LoginPrompt := False;
    Dts_SQLConnection.CommandTimeout := 0;
    Dts_SQLConnection.ConnectionTimeout := 0;
    Dts_SqlQuery.Connection := Dts_SQLConnection;
    Dts_SqlQuery.CommandTimeout := 0;
    Dts_SQLConnection.Open;
    try
      Form1.AddWorkListItem('连接本地数据服务器','连接成功');
      Dts_SqlQuery.SQL.Clear;
      Dts_SqlQuery.SQL.Add('Exec DataOutTrans :OutPath,:Storeid');
      Dts_SqlQuery.Parameters.ParamByName('OutPath').Value := Form1.IniSqlOutDir+'\';
      Dts_SqlQuery.Parameters.ParamByName('Storeid').Value := Form1.NetServerInfo.Strings[0];
      Form1.NetServerInfo.Strings[2] := '正在生成数据文件';
      Form1.TcpServer.Socket.Write(Form1.NetServerInfo,True);
      Form1.AddWorkListItem('执行数据导出','请稍候...');
      Dts_SqlQuery.ExecSQL;
      Dts_SqlQuery.Close;
      Dts_SQLConnection.Close;
      Form1.OutFileList.RefreshItems;
      if Form1.OutFileList.Items.Count <> Form1.IniSqlOutFileCount then
      begin
        Form1.AddWorkListItem('校验数据导出是否完整','数据导出不完整,请与管理员联系');
        if Form1.TCPServer.Connected then Form1.TCPServer.Disconnect;
        if Form1.FTPConnect.Connected then Form1.FTPConnect.Disconnect;
        Dts_SqlQuery.Close;
        Dts_SQLConnection.Close;
        Exit;
      end;
      Form1.NetServerInfo.Strings[2] := '生成数据文件成功';
      Form1.TcpServer.Socket.Write(Form1.NetServerInfo,True);
      Form1.AddWorkListItem('执行数据导出','导出成功');
      Result := True;
      Exit;
    except
      on E:Exception do
      begin
        Application.MessageBox(PChar(E.Message),'Error');
        if Form1.TCPServer.Connected then Form1.TCPServer.Disconnect;
        if Form1.FTPConnect.Connected then Form1.FTPConnect.Disconnect;
        Dts_SqlQuery.Close;
        Dts_SQLConnection.Close;
        Form1.AddWorkListItem('执行数据导出','执行错误,请与管理员联系');
        Exit;
      end;
    end;
  except
    if Form1.TCPServer.Connected then Form1.TCPServer.Disconnect;
    if Form1.FTPConnect.Connected then Form1.FTPConnect.Disconnect;
    Dts_SQLConnection.Close;
    Form1.AddWorkListItem('连接本地数据服务器','连接失败,请与管理员联系');
    Exit;
  end;
  CoUninitialize;
end;
上面是一段线程中的代码,在数据导出模块中有时会产生“NO Connect”错误,通过调式发现问题出在TADOQuery上,奇怪的是Dts_SQLConnection的Connected状态是True,查了很久都没搞清楚真正的原因在哪里,请各位高手指点下。

解决方案 »

  1.   

    在sqlserver 事件查看器中跟踪查一下看看哪里的问题
      

  2.   

    NO Connect是没有连接?看下数据库是否启动?
      

  3.   

    提示:‘NO Connect’=沒有連接,檢查一下是不是網絡的問題致使線程給DOWN掉了...
      

  4.   

    Dts_SQLConnection.Provider := 'SQLOLEDB';//多余
    Dts_SQLConnection.CommandTimeout := 0;//设置为0,太夸张了吧?
    Dts_SQLConnection.ConnectionTimeout := 0;//设置为0,太夸张了吧?
      

  5.   

    CommandTimeout 为0 很容易让系统崩溃的
      

  6.   

    当资源紧张的时候,而Dts_SQLConnection.CommandTimeout时间过短,就会报告无法连接。
    因为你没有提供足够的时间让他来处理连接问题。