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=用户数据库名
    好的代码像粥一样,都是用时间熬出来的
  • 相关阅读:
    第三天 python的初始编码,基本数据类型(int,str,bool),字符串的操作
    while循环,格式化输出,运算符,while...else...
    python的种类,变量,常量,基础数据类型,input,if条件语句
    JavaScript 之 web API
    传输层上的TCP和UDP
    应用层上的协议HTTP
    计算机网络通信
    grid布局
    使用classList和dataset实现tab切换
    JS之跨域
  • 原文地址:https://www.cnblogs.com/jijm123/p/14481509.html
Copyright © 2011-2022 走看看