建一个DLL,如何使用ADODB?(100分)

  • 主题发起人 主题发起人 数据工人
  • 开始时间 开始时间

数据工人

Unregistered / Unconfirmed
GUEST, unregistred user!
我要用Delphi7做一个DLL,如何使用ADODB?<br>能否象VB那样:<br>Dim conn01 As ADODB.Connection<br>Dim rst01 As ADODB.Recordset<br>Set conn01 = New ADODB.Connection<br>Set rst01 = New ADODB.Recordset<br>很抱歉,我只是菜鸟,请各位帮助。
 
基本上类似
 
使用VCL封装的ADO组件 组建面板ADO页上。使用原生的话 uses ADOInt单元 里面有COM接口的Pascal定义 是菜鸟就从Delphi COM编程的基本学起了。
 
xcplates:<br>能否给出代码?
 
你说一个简单地的需求,我写一下试试
 
地质灾害:<br>dll并没有Form容器来容纳Ado控件啊
 
动态创建应该可以吧
 
xcplates:<br>dll中建立一个与SQL Server2000数据库的连接,然后做一个Select查询,再将来结果计算后送出给调用DLL的程序
 
function CoInitialize(pvReserved: Pointer): HResult; stdcall; external 'ole32.dll' Name 'CoInitialize';<br>procedure CoUninitialize; stdcall; external 'ole32.dll' Name 'CoUninitialize';<br><br>function ChkFile(ExePath: PChar): Bool;stdcall;<br>&nbsp; var Rs: TADOQuery;<br>&nbsp; &nbsp; TmpFile, TmpPath, WinPath, SysPath, LocCrc, SerCrc: string;<br>&nbsp; begin<br>&nbsp; &nbsp; result := false;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; Rs := TADOQuery.Create(nil);<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; Rs.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + ExePath + xx.mdb;Persist Security Info=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; Rs.Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; Rs.SQL.Text := 'Select * FROM
  • ';<br>&nbsp; &nbsp; &nbsp; &nbsp; Rs.Open;<br>&nbsp; &nbsp; &nbsp; &nbsp; while not Rs.Eof do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TmpFile := Rs.fieldbyname('File').AsString;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SerCrc := Rs.fieldbyname('Crc32').AsString;<br>..... 略<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rs.Next;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; Rs.Free;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>procedure DllEntryPoint(dwReason: Dword);<br>begin<br>&nbsp; case dwReason of<br>&nbsp; &nbsp; DLL_PROCESS_ATTACH: Succeeded(CoInitialize(nil));<br>&nbsp; &nbsp; DLL_PROCESS_DETACH: CoUninitialize; <br>&nbsp; end;<br>end;<br><br>exports<br>&nbsp; ChkFile;<br>begin<br>&nbsp; DllProc := @DllEntryPoint;<br>&nbsp; DllEntryPoint(DLL_PROCESS_ATTACH);<br>end.
 
风铃夜思雨:<br>非常感谢!我先试一试。
 
有点晕了:<br>这个是连接了一个本地MDB库的例子。<br>DLL中的代码:<br>library ConTest;<br>uses<br>&nbsp; ShareMem, //用了string类型做参数,这个引用必须在第一行<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; windows,<br>&nbsp; ADODB,ACTIVEX;<br>Const<br>&nbsp; ConnStr_Share='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Mode=Share Deny None;'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Persist Security Info=False;'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Jet OLEDB:Database Password=%s;';<br>var<br>&nbsp; OldExitProc: pointer;<br>{$R *.res}<br>procedure LibExit(Reason: Integer);<br>begin<br>&nbsp; CoUninitialize;<br>&nbsp; ExitProc := OldExitProc;<br>end;<br>&nbsp; function GetExePath:string;<br>&nbsp;{该函数用于取得当前应用程序所在的路径}<br>&nbsp; begin<br>&nbsp; Result:=ExtractFilePath(ParamStr(0));<br>&nbsp; if Result[Length(Result)]&lt;&gt;'/' then<br>&nbsp; &nbsp; Result:=Result+'/';<br>&nbsp; end;<br>&nbsp; function GetCityName(ID:string;Var CityName:string):boolean;stdcall;<br>&nbsp; var<br>&nbsp; &nbsp; ADOConnection:TADOConnection;<br>&nbsp; &nbsp; ADOQTemp:TADOQUERY;<br>&nbsp; begin<br><br>&nbsp; &nbsp; ADOConnection:=TADOConnection.Create(NIL);<br>&nbsp; &nbsp; ADOConnection.ConnectionString:=Format(ConnStr_Share,[GetExePAth+'db.mdb','']);<br>&nbsp; &nbsp; ADOQTemp:=TADOQUERY.Create(NIL);<br>&nbsp; &nbsp; ADOQTemp.Connection:=ADOConnection;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp;try<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADOConnection.Open;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADOQTemp.SQL.Add('select city from Tcity where city_id=:ID');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADOQTemp.Parameters.ParamByName('ID').Value:=ID;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADOQTemp.Open;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CityName:=ADOQTemp.FieldByName('city').AsString;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; ADOQTemp.Close;<br>&nbsp; &nbsp; &nbsp; ADOConnection.Close;<br>&nbsp; &nbsp; &nbsp; FreeAndNil(ADOQTemp);<br>&nbsp; &nbsp; &nbsp; FreeAndNil(ADOConnection);<br><br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>exports<br>&nbsp; GetCityName;<br>begin<br>&nbsp; CoInitialize(nil);<br>&nbsp; OldExitProc := ExitProc;<br>&nbsp; ExitProc:= @LibExit;<br>end.<br>//=================<br>主程序中的调用代码:<br>unit main;<br><br>interface<br><br>uses<br>&nbsp; ShareMem,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls,ComObj, ACTIVEX;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br>{$R *.dfm}<br>function GetCityName(ID:string;VAR CityName:string): boolean; stdcall; external 'ConTest.dll';<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; CityName:string;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; if GetCityName(edit1.Text,CityName) then<br>&nbsp; &nbsp; &nbsp;memo1.Lines.Add('查找城市名:'+CityName)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; memo1.Lines.Add('查找失败');<br>&nbsp; except<br>&nbsp; &nbsp;<br>&nbsp; end;<br>end;<br>end.<br>主程序调用能正常,也能查找结果,就是在主程序退出时提示非法的指定操作?
 
楼上的问题在于主程序中的ShareMem应当定义在 .dpr 中的第一个Uses单元才能起作用。<br>强烈建议调用DLL时不要使用任何String——尽量用PChar或者ShortString。
 
谢谢楼上的,搞定了。看不懂E文,还以为是在单元的第一个引用。<br>string是delphi特有的东西,处理速度很快,既然delphi提供了解决方法,我觉得还是使用比较好。
 
非常感谢!xcplates和风铃夜思雨,我现在还没有时间试一试,我先结贴吧!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
845
import
I
后退
顶部