• Delphi ADOConnection连接


    unit Unit_DM;

    interface

    uses
    SysUtils, Classes, DB, ADODB,inifiles,windows,forms,controls;

     

    type
    TDM = class(TDataModule)
    ADOConn: TADOConnection;
    DS_BomFind: TDataSource;
    Qry_BomFind: TADOQuery;
    procedure DataModuleCreate(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    userIdStr,passwordStr,ServerNameStr,dataBaseNameStr,inifilePath:string;
    end;

    var
    DM: TDM;

    implementation

    {$R *.dfm}


    procedure TDM.DataModuleCreate(Sender: TObject);
    var
    conn:tinifile;

    begin
    //数据库连接设置。 conn.ini
    iniFilePath:=ExtractFilePath(application.ExeName); //获取INI文件路径。
    if inifilePath[length(inifilePath)]='' then
    begin
    iniFilePath:=iniFilePath+'conn.ini';
    conn:=tinifile.Create(iniFilePath);
    end
    else
    begin
    iniFilePath:=iniFilePath+'conn.ini';
    conn:=tiniFile.Create(inifilePath);
    end;
    if not FileExists(iniFilePath) then
    begin
    application.MessageBox('配置文件不存在!','提示',mb_OK) ;
    APplication.Terminate;
    end;
    // providerStr:=conn.ReadString('config','provider','');
    UserIDStr:=conn.ReadString('config','userID','');
    passwordStr:=conn.ReadString('config','password','');
    ServerNameStr:=conn.ReadString('config','ServerName','');
    dataBaseNameStr:=conn.ReadString('config','DataBaseName','');
    if (ServerNameStr='') or (DatabaseNameStr='') then
    begin
    application.MessageBox('数据库配置不正确,请重新配置!','提示',mb_ok);
    Application.Terminate;
    end;
    ADOconn.Close;
    ADOconn.ConnectionString:='';
    ADOconn.ConnectionString:='Provider=SQLOLEDB.1;User ID='+UserIDstr+';Password='+Passwordstr+ ';Data Source='+ServerNameStr+ ';Initial Catalog='+DatabaseNamestr;
    // adoconn.ConnectionString:='Provider='+Providerstr+';User ID='+UserIDstr+';Password='+Passwordstr+ ';Data Source='+DataSourceStr+ ';Initial Catalog='+DatabaseNamestr;

    try
    adoconn.Connected:=true;
    except
    application.MessageBox('数据库配置不正确,请重新配置!','提示',mb_ok);
    adoconn.Connected:=false;
    application.Terminate;
    end;
    end;

    end.

     

    下面是conn.INI文件内容:

    [config]
    UserID=sa
    Password=密码
    DataSource=服务器名或IP地址
    DatabaseName=用户数据库名

  • 相关阅读:
    JavaEye推荐:软件开发的葵花宝典 zt
    杨建:网站加速系统架构篇
    杨建:网站加速Cache为王篇
    整理:不用ACE你不知道ACE有多烂,给饱受ACE折磨的弟兄们散分了。
    jQuery对select操作 dodo
    easyui事件和方法的调用 dodo
    .Net 下利用ICSharpCode.SharpZipLib.dll实现文件压缩、解压缩 dodo
    使用Jquery EasyUi常见问题解决方案 dodo
    如何切分用户故事 dodo
    什么是产品Backlog,什么是Sprint Backlog? dodo
  • 原文地址:https://www.cnblogs.com/jijm123/p/12002013.html
Copyright © 2020-2023  润新知