来源:www.cncfan.com | 2006-1-16 | (有2686人读过)
关键词:DLL ADO连接 //dll主程序 library erpsend; uses SysUtils, Classes, Forms, msgdata in 'msgdata.pas' {data: TDataModule}; {$R *.res} function msgsend(MsgNo:Integer;AccNo:String;sAccNo:String;sName:String;sDepNo: String;sDepName:String;SysName:String;msg:string):Integer;stdcall; begin try //根据参数更新SQL server with data.Tmsg do begin append; FieldByName('MsgNo').AsInteger:=MsgNo; FieldByName('AccNo').AsString:=AccNo; FieldByName('sAccNo').AsString:=sAccNo; FieldByName('sName').AsString:=sName; FieldByName('sDepNo').AsString:=sDepNo; FieldByName('sDepName').AsString:=sDepName; FieldByName('SysName').AsString:=SysName; FieldByName('sMsgTxt').AsString:=msg; FieldByName('sTime').AsString:=FormatDateTime('yyyy/mm/dd hh:mm',now); post; end; msgsend:=1;//成功返回1 except msgsend:=0;//失败返回0 end; end; exports msgsend INDEX 1; begin Application.CreateForm(TData, Data);//dll调用时自动初始化数据ado连接 end. //数据模块DataModule单元 unit msgdata; interface uses SysUtils, Classes, DB, ADODB,Dialogs; type Tdata = class(TDataModule) ADOCon: TADOConnection; Tmsg: TADOTable; procedure DataModuleCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var data: Tdata; implementation {$R *.dfm} procedure Tdata.DataModuleCreate(Sender: TObject); begin try with ADOCon do //动态建立与sql server 连接 begin ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=erpmsg;Data Source=bill'; {其中:erpmsg为数据库名,bill为服务器名} Open('erpuser','erp'); {其中:erpuser为用户名,erp这密码} end; tmsg.TableName:='msgtxt'; tmsg.active:=true; {若此处读取外部配置文件,如ini文件,可实现SQL主机、用户、密码等信息的灵活修改 } except SHOWMESSAGE('连接数据服务器失败!'); end; end; end. 这样就可以在一些对ado支持不好的语言如vfp等中实际ado数据连接与更新,避免了客户端odbc设置,不过在用户计算机上要设一下sql客户端,最简单的办法是写入如下注册表信息: Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client] [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo] "BILL"="DBMSSOCN,BILL,1433" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\DB-Lib] [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\TDS] "BILL"="7.0" (以上为Win2000注册表格式,Win98类似)
|