应该可以,看看以下对你有没有用:
60. 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類似)