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=用户数据库名
好的代码像粥一样,都是用时间熬出来的