mORMot学习笔记2-2种方式查询数据

我只是一个虾纸丫 提交于 2019-11-30 10:03:10

本例使用SqlServer,所以需要引用三个单元SynCommons,SynDB,SynOleDb

第一种方式结果放入Memo控件

procedure TForm1.Button1Click(Sender: TObject);
var
  DbConn: TOleDBMSSQLConnectionProperties;
  strSql: string;
  rows: ISQLDBRows;begin
  DbConn := TOleDBMSSQLConnectionProperties.Create('127.0.0.1', 'ReportServer', 'sa', 'Sa123');
  strSql := 'SELECT r.RoleName, r.Description FROM Roles AS r';
  rows := DbConn.ExecuteInlined(strSql, True);
  if rows <> nil then
  begin
    memo1.Clear;
    memo1.Lines.BeginUpdate;    while rows.Step() do
    begin
      Memo1.Lines.Add(rows.ColumnString('RoleName') + '-' + rows.ColumnString('Description'));
    end;
  end;
  Memo1.Lines.EndUpdate;
end;

第二种方式,返回数据集到DBGrid控件

procedure TForm1.Button2Click(Sender: TObject);
var
  DbConn: TOleDBMSSQLConnectionProperties;
  ds: TSynDBDataSet;
begin
  DbConn := TOleDBMSSQLConnectionProperties.Create('127.0.0.1', 'ReportServer', 'sa', 'Sa123');
  ds := TSynDBDataSet.Create(nil);
  ds.Connection := DbConn;
  ds.CommandText := 'SELECT r.RoleName, r.Description FROM Roles AS r';
  ds.Open;
  DataSource1.DataSet := ds;
  //ds不能在这里释放不然结果就不显示了
end;

两种方式执行结果

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!