DBMNG数据库管理与应用

书籍是全世界的营养品。生活里没有书籍,就好像没有阳光;智慧里没有书籍,就好像鸟儿没有翅膀。
当前位置:首页 > 经验分享 > Delphi

DelphiADO增删查改实例

var
ADO: TADOQuery;
ADOConn: TADOConnection;

//---------------建立ADO
ADO := TADOQuery.Create;

*****************************************************************************
 Bof Eof  // 头尾指针
while not Eof do
      begin
        cbStandardNO.Items.Add(FieldByName('STANDARDNO').AsString);
        Next;
      end;
*****************************************************************************

***ADO链接数据库  
// 在ADOConn的属性中设置LoginPrompt为False Connected为Fasle
S := 'Provider=OraOLEDB.Oracle.1;Password=' + SPassword +
    ';Persist Security Info=True;User ID=' + SUserName +
    ';Data Source=' + SIP;+';PLSQLRSET=1'//--------返回数据集  PLSQLRSET=1
try
    ADOConn.Connected := false;
    ADOConn.ConnectionString := S;
    ADOConn.Connected := true;
except
end;
ADO.Connection := ADOConn;
*****************************************************************************
//------------------------------------------------------------------------------查询
with ADO do
           begin
             if Active then Close;
             SQL.Text := 'SELECT KEYWORD FROM TELEGRAMPLTCM WHERE  ' +
                         ' TELEGRAMID=:TELEGRAMID  AND REC_NO > (SELECT REC_NO FROM TELEGRAMPLTCM ' +
                         'WHERE KEYWORD = :KEYWORD AND TELEGRAMID = 1001)';
             Parameters.ParamByName('KEYWORD').Value := CoilNo;
             Parameters.ParamByName('TELEGRAMID').Value := 1001;
             Open;
             iCount := RecordCount;
           end;
      //-----------------------循环得到值
      for i := 0 to iCount-1 do
        begin
            Application.ProcessMessages;
            KEYWORD   := FieldByName('KEYWORD').AsInteger;
           tr.TelID   := FieldByName('TELEGRAMID').AsInteger;
           Next;
       end;

//--更新 删除
try
    with ADO do
    begin
      if Active then Close;
      SQL.Text := 'UPDATE PLANCOIL SET L2TAG=:L2TAG,L2ERROR=:L2ERROR WHERE COILNO=:COILNO';
      Parameters.ParamByName('L2TAG').Value := 1;
      Parameters.ParamByName('L2ERROR').Value := Spare;
      Parameters.ParamByName('COILNO').Value := CoilNo;

      ExecSQL;
      Result := True;
    end;
  except
    on E: Exception do
     ErrorText := E.Message;
  end;

//-------------释放ADO
   FreeAndNil(ADO);
*****************第二种方法Open;EDIT;DELETE;LOCATE;
//-----------------------------------------------------------------插入
    try
    with ADOQuery1 do
    begin
      Close;
      SQL.Text:= 'select * from coilqm where rownum = 1 ';
      Open;
      Append;
      FieldByName('COILNO').Value:= '9A0054';
      FieldByName('QMRESULT').Value:= 0;
      FieldByName('CHECKER').Value:= '张三';
      FieldByName('CHECKTIME').Value:= now;
      FieldByName('MEMO').Value:= 'NG';
      Post;
    end;
  finally
  end;
//-----------------------------------------------------------------修改
    try
    with ADOQuery1 do
    begin
      Close;
      SQL.Text:= 'select * from coilqm where rownum = 1 ';
      Open;
      EDIT;
      FieldByName('COILNO').Value:= '9A0054';
      FieldByName('QMRESULT').Value:= 9;
      FieldByName('CHECKER').Value:= '张三';
      FieldByName('CHECKTIME').Value:= now;
      FieldByName('MEMO').Value:= 'NG';
      Post;
    end;
  finally
  end;
//---------------------------------------------------------------删除
    try
    with ADOQuery1 do
    begin
      Close;
      SQL.Text:= 'select * from coilqm where rownum = 1 ';
      Open;
      DELETE;
    end;
  finally
  end;
//--------------------------------------------------------------查询
    try
    with ADOQuery1 do
    begin
      Close;
      SQL.Text:= 'select * from coilqm where rownum = 1 ';
      Open;
      LOCATE('COILNO','9A0054',[loCaseInsensitive,loPartialKey]);
      ShowMessage(FieldByName('CHECKER').Value);
    end;
  finally
  end;

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号