设为首页 加入收藏

TOP

mormot2笔记(一) 连接数据库
2023-08-26 21:08:35 】 浏览:248
Tags:mormot2 笔记 连接数
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.
View Code

 

注:由于 ProviderName 属性是只读的,所以在create中初始化一下(能不修改源代码就不改)

mormot.db.sql.oledb   单元中定义了mssql相关类,如果连接其它数据库则要引用相对应的文件。

 

ODBC连接,注释的几种都是可行的:

  FDBProp := TSqlDBOdbcConnectionProperties.Create('321', '', 'sa', '12345');   //已经建好321连接,使用OLEDB驱动,不能省略【用户名】和【密码】
  FDBProp.Dbms := TSqlDBDefinition.dMSSQL;
//  FDBProp := TODBCProp.Create('123', '', 'sa', '12345'); //同上 123使用ODBC驱动, 不能省略【用户名】和【密码 】
//  FDBProp := TODBCProp.Create('', 'DSN=123;UID=sa;PWD=Sa12345;DATABASE=MyDB', '', '');  //使用ODBC驱动,可以省略【用户名】和【密码】
//  FDBProp := TODBCProp.Create('', 'DRIVER=ODBC Driver 17 for SQL Server;UID=sa;PWD=12345;Trusted_Connection=No;SERVER=192.168.1.50;MARS_Connection=yes;DATABASE=MyDB', '', ''); //这样也可以
//  FDBProp.SqlDriverConnectPrompt := TRUE;   //会弹出ODBC设置对话框

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇没有了 下一篇mormot2 笔记(二) Http服务的简单..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目