zoukankan      html  css  js  c++  java
  • Delphi ADOConnection连接 sqlserver

    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=密码
    ServerName=服务器名或IP地址
    DatabaseName=用户数据库名
    好的代码像粥一样,都是用时间熬出来的
  • 相关阅读:
    关于字符函数的一些应用总结
    常用数字函数简单举例
    dump浅析B树索引
    pl/sql developer 登陆sys用户
    关于oracle11g简单的网络配置
    关于oracle数据库读取数据的三种方式
    创建表
    SQL*Plus 与数据库的交互
    Oracle编程艺术--配置环境
    MongoDB--(NoSQL)入门介绍
  • 原文地址:https://www.cnblogs.com/jijm123/p/14481509.html
Copyright © 2011-2022 走看看