我需要一个使用adocontion的例子,以避免MySQL服务器已经消失

sgtfey8w  于 2022-10-16  发布在  Mysql
关注(0)|答案(2)|浏览(138)

我有一个使用远程数据库的桌面应用程序,不幸的是,我不能更改那里的内存限制,当我使用本地数据库或局域网数据库时,我的应用程序还可以。但在使用远程数据库时,我经常发现错误“MySQL服务器已经消失了‘我的假设是我没有很好地使用adoConnection。我做了ado.mittrans和ado.eginans,但我没有做ado.lose或ado.fre.我需要一个这样的例子……我使用的是ado的什么功能,我把它们放在哪里?每个事务,包括显示给数据库网格?或别的什么……我的主机配置是:

> safe mode = Off (tidak dapat diubah)

memory_limit = 256M (MAXIMUM)

max_execution_time = 30 (MAXIMUM in seconds)

max_input_time = 60 (MAXIMUM in seconds)

post_max_size = 64M (MAXIMUM)

upload_max_filesize = 64M (MAXIMUM)

enable_dl = Off (tidak dapat diubah)

Stimultan connection: 25 connections/koneksi

**Time out without activity: 60 sec**

我想知道在我的项目中。我没有保持我的连接处于活动状态

以下是我的代码:

Procedure TForm1.show;
Begin
with query do begin
sql.clear;//menghapus query yang masih ada jika ada
sql.Text:='select * from daftar where tanggal = curdate() order by id asc';
open;//membuka query
end;
end;
  function AddLeadingZeroes(const aNumber, Length : integer) : string;
begin
   result := SysUtils.Format('%.*d', [Length, aNumber]) ;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a:string;
begin
a:=AddLeadingZeroes(strtoint(edit3.Text),4);
 edit3.text:=a;
 edit2.text:=AnsiUpperCase(edit2.text);
if (edit2.text='') or (edit3.text='')
  then
    begin
      showmessage('Nomor Medrek HARUS diisi terlebih dahulu');
      edit2.setfocus
    end
  else
    begin
      con.BeginTrans;
      query1.SQL.Clear;
      query1.SQL.text :='Select Nama from medrek where LETTER='+QuotedStr(edit2.text)+'AND ID='+QuotedStr(edit3.text)+';';
      Screen.Cursor:=crHourGlass;
      query1.open;
      If query1.RECORDCOUNT = 0
        Then
          begin
            ShowMessage('NOMOR MEDREK BELUM TERDAFTAR') ;
            edit1.Color := clMaroon;
            edit1.clear ;
            con.CommitTrans;
          end
        ELSE
          begin
            edit1.text := query1.FieldByName('Nama').AsString;
            edit1.Color := clYellow;
            query.Requery();
            con.CommitTrans;
          END;
    query1.close;
    Screen.Cursor:=crDefault;

    end

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

if (edit1.text='') or (combobox1.text='') or (combobox2.text='') or (combobox3.text='')  or (combobox4.text='') then
    begin
    MessageDlg('DATA BELUM LENGKAP !',mtError, mbOKCancel, 0);
    end
      else
        begin
        Screen.Cursor:=crHourGlass;
            con.BeginTrans;
            with query do
            begin
                sql.Clear;
                SQL.Text:='INSERT INTO daftar (tanggal,NO_MEDREC, NAMA, lAMA, STATUS, KETERANGAN, POLI) VALUES ("'+formatdatetime('yyyy-mm-dd',today())+'","'+concat(Edit2.Text,'-',edit3.text)+'","'+Edit1.Text+'","'+combobox1.text+'","'+combobox3.text+'","'+combobox2.text+'","'+combobox4.text+'")';
                execsql;//untuk mengeksekusi perintah SQL
                Showmessage('Data berhasil disimpan');
                show;
            end;
        con.CommitTrans;
        Screen.Cursor:=crDefault;
        button1.Enabled:=false;
        button2.Enabled:=false;
          query1.close;
        end;
end;
procedure TForm1.ComboBox3Click(Sender: TObject);
begin
IF combobox3.text='UMUM'  THEN
  begin
   EDIT5.Color:=clBlack;
   EDIT5.CLEAR;
   EDIT5.ReadOnly := true
   END
   ELSE
   BEGIN
   EDIT5.Color:=clWHITE   ;
   EDIT5.ReadOnly := FALSE
   END

END;
procedure TForm1.Button3Click(Sender: TObject);
begin
con.connected:=true;
 with query do
            begin
                sql.Clear;
                SQL.Text:='Select * from daftar where tanggal = curdate()';
                execsql;//untuk mengeksekusi perintah SQL

                show;
                end;

button1.Enabled:=TRUE;
button2.Enabled:=TRUE;
edit1.Clear;
edit2.Clear;
edit3.Clear;
edit5.Clear;
combobox1.ClearSelection;
combobox2.ClearSelection;
Combobox3.ClearSelection;
combobox4.ClearSelection;
edit2.SetFocus;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
con.connected:=true;
 with query do
            begin
                sql.Clear;
                SQL.Text:='Select * from daftar where tanggal = curdate()';
                execsql;//untuk mengeksekusi perintah SQL

                show;
                end;
Show;
end;

procedure TForm1.P2Click(Sender: TObject);
begin
FORM2.SHOWMODAL
end;

procedure TForm1.P1Click(Sender: TObject);
begin
form3.showmodal;
end;

procedure TForm1.CETAKClick(Sender: TObject);
begin
  form4.show;
end;

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if DBGrid1.DataSource.DataSet.RecNo mod 2 =0 then
DBGrid1.Canvas.Brush.Color := clskyBlue; //pilih warnanya
DBGrid1.DefaultDrawColumnCell(rect, datacol, column, state);

if DataSource1.DataSet.RecNo > 0 then
begin
if Column.Title.Caption = 'No.' then
DBGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top, IntToStr(DataSource1.DataSet.RecNo));
end;
end;

procedure TForm1.CARIHISTORYPASIEN1Click(Sender: TObject);
begin
form5.show;
end;

End.

我将ado属性直接设置为它的属性。不使用代码、提供程序进行adoConnection。Provider=MSDASQL.1并且在adoConnection.Connected上选中=TRUE
谢谢。。

sdnqo3pr

sdnqo3pr1#

MySQL服务器已离开错误的最常见原因是服务器超时并关闭了连接。在这种情况下,您通常会得到以下错误代码之一(您得到的错误代码取决于操作系统)。
由于这个原因,我收到了这个错误
您在服务器端遇到超时,并且客户端的自动重新连接被禁用(MySQL结构中的重新连接标志等于0)。
但此错误的完整原因在下面的链接中描述
http://dev.mysql.com/doc/refman/5.7/en/gone-away.html

o3imoua4

o3imoua42#

就像火影忍者解释的那样,这可能是暂停了。我有自己的函数,它在ADOQuery中执行SQL,并在失败时重新连接到数据库。
如果失败三次以上,则会引发异常,并显示消息和我试图执行的查询。
参数Ex将执行设置为Open或ExecSQL。
使用ADOQuery调用该函数,如果要执行SQL,则调用TRUE;如果要打开查询,则调用FALSE。

function ExecuteMySQL(var Q: TADOQuery; Ex:boolean):integer;
const
     MaxNoOfTry : integer = 3;
var
     NoOfTry : integer;
     msg     : string;

begin
  Result:= 0;
  NoOfTry := 0;
  while (NoOfTry < MaxNoOfTry) do
  begin
    inc(NoOfTry);
    try
      if Ex then
        Result := Q.ExecSQL
      else
      begin
        msg := Q.SQL.Text;
        Q.Open;
        Result := Q.RecordCount;
      end;
      NoOfTry := MaxNoOfTry+1;
    except
      on E : Exception do
      begin
        Result := -1;
        Msg := E.Message;
        Q.Connection.Close;
        Q.Connection.Open;
      end;
    end;
  end;
  if Result = -1 then
    ShowMessage(Msg+chr(13)+chr(13)+Q.SQL.Text);
end;

相关问题