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设置对话框

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/Jiaojiawang/p/17631630.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!