mormot笔记一 连接数据库

unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, mormot.db.sql, mormot.db.core, mormot.db.sql.oledb, mormot.core.base; type TForm1 = class(TForm) StringGrid1: TStringGrid; Memo1: TMemo; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; TSqlDBOleDBMSSQLConnectionPropertiesEx = class(TSqlDBOleDBMSSQLConnectionProperties) public constructor Create(const aServerName, aDatabaseName, aUserID, aPassWord: RawUtf8); override; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin var conPool := TSqlDBOleDBMSSQLConnectionPropertiesEx.Create('192.168.1.2', 'xb', 'sa', '223344'); var dataTable := conPool.ExecuteInlined('select top 10 * from TestTable', true); StringGrid1.ColCount := dataTable.ColumnCount; StringGrid1.RowCount := 100; var rowIndex := 1; while dataTable.Step do begin for var I := 0 to dataTable.ColumnCount-1 do begin StringGrid1.Cells[I, rowIndex] := dataTable.ColumnString(I); end; inc(rowIndex); end; end; { TSqlDBOleDBMSSQLConnectionPropertiesEx } constructor TSqlDBOleDBMSSQLConnectionPropertiesEx.Create(const aServerName, aDatabaseName, aUserID, aPassWord: RawUtf8); begin fProviderName := 'SQLOLEDB.1'; inherited Create(aServerName,aDatabaseName, aUserID, aPassWord); end; end.
注:由于 ProviderName 属性是只读的,所以在create中初始化一下(能不修改源代码就不改)
mormot.db.sql.oledb 单元中定义了mssql相关类,如果连接其它数据库则要引用相对应的文件。