• Delphi ADOQuery Demo


    编辑器加载中..

    procedure TForm2.FormCreate(Sender: TObject);
    const
    { Connection string }
    ConnString =
    'Provider=SQLOLEDB.1;Persist Security Info=False;' +
    'User ID=%s;Password=%s;Data Source=%s;Use Procedure for Prepare=1;' +
    'Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;'+
    'Tag with column collation when possible=False';

    { SQL Query }
    SQLStr = 'SELECT * FROM customer WHERE customer_id = :AnId;';

    { User access }
    UserName = 'db_user_name';
    PassWord = 'db_pass_word';
    Server = 'my.db.server';

    var
    ADOConn : TADOConnection;
    ADOQuery : TADOQuery;
    DataSrc : TDataSource;
    Param : TParameter;

    begin
    { Create an ADO connection. }
    ADOConn := TADOConnection.Create(Self);
    { Set up the provider engine }

    { Set up the connection string. }
    ADOConn.ConnectionString := Format(ConnString,
    [UserName, PassWord, Server]);

    { Disable login prompt. }
    ADOConn.LoginPrompt := False;

    try
    ADOConn.Connected := True;
    except
    on e: EADOError do
    begin
    MessageDlg('Error while connecting', mtError,
    [mbOK], 0);

    Exit;
    end;
    end;

    { Create the query. }
    ADOQuery := TADOQuery.Create(Self);
    ADOQuery.Connection := ADOConn;
    ADOQuery.SQL.Add(SQLStr);

    { Update the parameter that was parsed from the SQL query: AnId. }
    Param := ADOQuery.Parameters.ParamByName('AnId');
    Param.DataType := ftInteger;
    Param.Value := 1;

    { Set the query to Prepared--it will improve performance. }
    ADOQuery.Prepared := true;

    try
    ADOQuery.Active := True;
    except
    on e: EADOError do
    begin
    MessageDlg('Error while doing query', mtError,
    [mbOK], 0);

    Exit;
    end;
    end;

    { Create the data source. }
    DataSrc := TDataSource.Create(Self);
    DataSrc.DataSet := ADOQuery;
    DataSrc.Enabled := true;

    { Finally, initialize the grid. }
    DBGrid1.DataSource := DataSrc;
    end;

    很简单的Demo。。。。有几个亮点,以便学习,第一次 首页 说明Delphi,没有消亡。。。

  • 相关阅读:
    动画效果(二)
    动画效果(一)
    高级事件(二)
    高级事件(一)
    事件对象(二)
    事件对象(一)
    使用jquery ajax代替iframe
    SQL语句汇总(终篇)—— 表联接与联接查询
    SQL语句汇总(三)——聚合函数、分组、子查询及组合查询
    SQL语句汇总(二)——数据修改、数据查询
  • 原文地址:https://www.cnblogs.com/chenmfly/p/2398261.html
Copyright © 2020-2023  润新知