小弟写了一个工具用于同步两个视频网站的数据。程序是保持长期运行的类似服务端程序一样开着不关。。它会定时从服务器与本地数据对比检查出更新内容并且下载数据以及相关视频文件,截图文件。 待下载完成后插入本地数据库。 用的是ODBC。现在发现了一个问题。。 首先程序刚启动时, 任务管理器中查看进程占用内存是12M左右, 几个小时后, 便会降为6M左右。。 貌似在这个时候, 如果服务器上再有数据更新时, 则会插入数据库失败。。我想问的是, TADOConnection对象是不是长时间如果没有使用, 会自动断开连接, 造成插入数据失败。我找不到原因。function OpenConnect(Conn: TADOConnection): Boolean;
var
  rs: TADOQuery;
  strConnString: string;
begin
  Result := True;
  strConnString := 'Driver={MySQL ODBC 3.51 Driver};CHARSET=gbk;SERVER=' + DB_SERVER + ';PORT=' + DB_PORT + ';USER=' + DB_USER + ';PASSWORD=' + DB_PASS + ';DATABASE=' + DB_NAME + ';';
  Conn.ConnectionString := strConnString;
  try
    Conn.LoginPrompt := True;
    Conn.Open;
  except
    Result := False;
  end;  if Conn.Connected then begin
    rs := TADOQuery.Create(nil);
    rs.Connection := Conn;
    rs.SQL.Text := 'SET NAMES gbk;';
    rs.ExecSQL();    {
    rs.Close();
    rs.SQL.Text := 'SET character_set_client = utf8;';
    rs.ExecSQL();  
    rs.Close();
    rs.SQL.Text := 'SET character_set_connection = utf8;';
    rs.ExecSQL();
    rs.Close();
    rs.SQL.Text := 'SET character_set_database = utf8;';
    rs.ExecSQL();
    rs.Close();
    rs.SQL.Text := 'SET character_set_results = GBK;';
    rs.ExecSQL();    
    rs.Close();
    rs.SQL.Text := 'SET character_set_server = utf8;';
    rs.ExecSQL();
    }    rs.Close;
    rs.Free;
  end;
end;我的程序是有一个全局变量 conn: TADOConnection, 在程序启动时直接连接数据库, 成功才继续运行。另外还有一个问题, 我实在是找不到关于Conn.LoginPrompt这个属性的说明, 网上找到的代码这里都赋值False, 目前我刚刚修改成True在测试。谢谢各位了。。

解决方案 »

  1.   

    帮助里面搜索“TCustomConnection,LoginPrompt”(不包含引号)
      

  2.   

    问题1:TADOConnection对象是不是长时间如果没有使用, 会自动断开连接, 造成插入数据失败
    不会.
    另外,TADOConnection有个KeepConnection,一般可设为true
    Specifies whether an application remains connected to a database even if no datasets are open.
    property KeepConnection: Boolean;
    Description
    Use KeepConnection to specify whether an application remains connected to a database even if no associated dataset components are currently active. When KeepConnection is True (the default) the connection is maintained. For connections to remote database servers, or for applications that frequently open and close datasets, set KeepConnection to True to reduce network traffic, speed up applications, and avoid logging in to the server each time the connection is reestablished.
    When KeepConnection is False a connection is dropped when there are no open datasets. Dropping a connection releases system resources allocated to the connection, but if a dataset is later opened that uses the database, the connection must be reestablished and initialized.
    问题2:我实在是找不到关于Conn.LoginPrompt这个属性的说明, 网上找到的代码这里都赋值False, 目前我刚刚修改成True在测试。
    控件属性上就有LoginPrompt,选中按F1就有HELP了,它是在连接数据库时,是否提示输入用户名/口令
    Specifies whether a login dialog appears immediately before opening a new connection.property LoginPrompt: Boolean;DescriptionSet LoginPrompt to True to provide login support when establishing a connection. LoginPrompt controls two things: the occurrence of the OnLogin event, and the appearance of a default login dialog that prompts users for a name and password when you add DBLogDlg to your uses clause. When the OnLogin event occurs and when the default login dialog appears depend on the type of connection component: For TDatabase, TIBDatabase, and TSQLConnection, the dialog appears after the BeforeConnect event and before the AfterConnect event, unless you supply an OnLogin event handler. If there is an OnLogin event handler, that event occurs in place of the login dialog, and there is no need to add DBLogDlg to your uses clause. If correct values for the user name and password are not supplied in the dialog or by the OnLogin event handler, the connection fails. The OnLogin event does not fire unless LoginPrompt is set to True. For TADOConnection components, the dialog appears after the OnWillConnect event and before the BeforeConnect event. If there is an OnLogin event handler, that event occurs after the login dialog. If you do not add DBLogDlg to your uses clause, the OnLogin event occurs but there is no default login dialog. If correct values for the user name and password are not supplied in the dialog or by the OnLogin event handler, the connection fails. The OnLogin event does not fire unless LoginPrompt is set to True. For DataSnap connection components, the dialog appears after the OnGetUsername event and before the BeforeConnect, AfterConnect, and OnLogin events. If the user cancels from the login dialog, no attempt is made to open a connection.When LoginPrompt is False, the application must supply user name and password values programmatically:For TDatabase, the user name and password can be supplied as USER NAME and PASSWORD parameters in the Params property.
    For TADOConnection, the user name and password can be supplied as the ConnectionString property.
    For TSQLConnection, the user name, password, and database can be supplied as UserName, Password, and Database parameters in the Params property, or provided as connection parameters associated with the connection name.
    For DataSnap connection components, there is no built-in use for the user name and password supplied by the login dialog. (The UserName and Password properties of TWebConnection are unrelated).Warning: Storing hard-coded user name and password entries as property values or in code for an OnLogin event handler can compromise server security.
      

  3.   

    提示输入用户名/口令什么意思啊?
    我改成true了也没有什么提示
      

  4.   

    如果LoginPrompt为true,那么在程序连接数据时,无论你的连接串中是否包括用户名/口令,它都会要求你输入连接数据库的用户名/口令
    如果LoginPrompt为false,那么它会用连接串中的用户名/口令而不提示
    HELP中写的很清楚
      

  5.   

    你的程序,如果改成LoginPrompt为true也没提示,可能的问题是连接串或ODBC本身出错了
    try
        Conn.LoginPrompt := True;
        Conn.Open;
      except
        Result := False;
      end;
    因为你用了try...except...所以如果出错会被程序截获,在调试时先试试不用try...except...
      

  6.   

    谢谢。。 难怪要用false。。
    另外, 我经过测试, 我把KeepConnection改为True, 还是不能插入数据库了。
      

  7.   

    最后发现是长时间后tcp连接断开解决办法是每次使用前conn断开重连。