dac mssql server

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
  FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, FireDAC.UI.Intf,
  FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Phys, FireDAC.VCLUI.Wait,
  FireDAC.Phys.MSSQLDef, FireDAC.Phys.ODBCBase, FireDAC.Phys.MSSQL,
  FireDAC.Comp.UI, Data.DB, FireDAC.Comp.Client, FireDAC.Comp.DataSet,
  Data.Win.ADODB, Vcl.Grids, Vcl.DBGrids, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    ds1: TDataSource;
    dbgrd1: TDBGrid;
    FDQuery1: TFDQuery;
    con1: TFDConnection;
    FDGUIxWaitCursor1: TFDGUIxWaitCursor;
    FDPhysMSSQLDriverLink1: TFDPhysMSSQLDriverLink;
    btn1: TButton;
    btn2: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{
Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;
User ID=sa;Initial Catalog=HY_STD;Data Source=.
}
procedure TForm1.btn1Click(Sender: TObject);
var
  oParams: TStrings;
begin
  oParams := TStringList.Create;
  oParams.Add('Server=127.0.0.1');
  oParams.Add('Database=HY_STD');
  oParams.Add('OSAuthent=Yes');
  FDManager.AddConnectionDef('MSSQL_Connection', 'MSSQL', oParams);
  con1.ConnectionDefName := 'MSSQL_Connection';

  con1.Connected := True;
  FDQuery1.Open('select * from tb_userInfo');
end;

//sqlserver网络配置-> sqlserver的协议->tcp/ip 启用
//(默认是不启用的,若连接的数据库是mssql,则必须手工去启用,
//否则连接不上。)

procedure TForm1.btn2Click(Sender: TObject);
begin
  with con1.Params do
  begin
    Add('DriverID=MSSQL');
    Add('Database=HY_STD');
    Add('User_Name=sa');
    Add('Password=123');
    Add('Address=127.0.0.1');
    Add('CharacterSet=UTF8'); // 否则中文乱码
  end;
  con1.Connected := True;
  FDQuery1.Open('select * from tb_userInfo');
end;


end.
书搞进脑袋 创新 创造; 积极
原文地址:https://www.cnblogs.com/tobetterlife/p/12171174.html