怎样编写一个dll,用ado访问sql server 中的数据? 能给个例子吗!谢谢!

解决方案 »

  1.   

    注意两个问题:1、在dll中使用ADO连接数据库需要在单元的初始化节部分加入CoInitialize, 
      因为ADO用到了COM;当然,finalization里面要相应加入CoUninitialize。 
    2、在dll导出的函数中不能直接使用string,若一定要用,要在dll工程文件的 
      Uses部分的最前面加上对ShareMem单元的引用。
      

  2.   

    下面是 DLL完整源程序和主叫程序完整源程序。包括以下四个文件: Project1.DPR {主叫程序}
    Unit1.PAS {主叫程序单元} 
    Project2.DPR {DLL}
    Unit2.PAS {DLL单元}
    {---------- DLL 主程序 Project2.DPR ----------}library Project2;uses
    SysUtils,
    Classes,
    Unit2 in 'Unit2.pas' {Form1};{$R *.RES}{ 下面的语句用于向调用该 DLL的程序提供调用接口 }
    exports
    DoTest; { 过程来自单元Unit2 }begin
    end.
    {---------- DLL中的单元 Unit2.PAS ----------}unit Unit2;interfaceuses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    Db, ADODB, StdCtrls, Menus;type
    TForm1 = class(TForm)
    ADOConnection1: TADOConnection;{ 本地数据库连接 }
    Memo1: TMemo; { 用于显示信息 }
    private
    public
    end;{ 该过程向外提供 }
    procedure DoTest(H: THandle; { 获得调用者的句柄 }
    AConn: TADOConnection;{ 获得调用者的数据库连接 }
    S: string; { 获得一些文本信息 }
    N: Integer); { 获得一些数值信息 }
    cdecl; { 指定调用协议 } implementation{$R *.DFM}procedure DoTest(H: THandle; AConn: TADOConnection; S: string; N: Integer);
    begin
    Application.Handle := H; { 将过程的句柄赋值为调用者的句柄 }
    { 上面语句的作用在于, DLL的句柄和调用者的句柄相同,在任务栏中就不会 }
    { 各自出现一个任务标题了。 }
    with TForm1.Create(Application) do try{ 创建窗体 }
    Memo1.Lines.Append('成功调用'); { 显示一行信息 }
    ADOConnection1 := AConn; { 获得数据库连接的实例 }
    Memo1.Lines.Append(
    ADOConnection1.ConnectionString +
    ' - ' + S + ' - ' + IntToStr(N)); { 根据得到的参数显示另一行信息 }
    ShowModal; { 模式化显示窗体 }
    finally
    Free; { 调用结束时销毁窗口 }
    end;
    end;end.
    {---------- 调用者 Project1.DPR,很普通的工程文件 ----------}program Project1;uses
    Forms,
    Unit1 in 'Unit1.pas' {Form1};{$R *.RES}begin
    Application.Initialize;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
    end.
    {---------- 调用者单元Unit1.PAS ----------}unit Unit1;interfaceuses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    StdCtrls, Db, ADODB;type
    TForm1 = class(TForm)
    Button1: TButton; { 按此按钮进行调用 }
    ADOConnection1: TADOConnection; { 本地数据库连接,将传递给 DLL }
    procedure Button1Click(Sender: TObject);{ 调用 DLL}
    private
    public
    end;var
    Form1: TForm1;implementation{$R *.DFM}{ 外部声明必须和 DLL中的参数列表一致,否则会运行时错误 }
    procedure DoTest(H: THandle; { 传递句柄 }
    AConn: TADOConnection; { 传递数据库连接 }
    S: string; { 传递文本信息 }
    N: Integer); { 传递数值信息 }
    cdecl; { 指定调用协议 }
    external 'Project2.dll';{ 指定过程来源 }{ 调用过程 }
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    DoTest(Application.Handle,
    ADOConnection1,
    'Call OK',
    256);
    end;end.
     
      

  3.   

    ---------------------
    //下面是DLL程序
    //目的:向TchInfo表中添加数据
    library TeacherInfo;{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }uses
      ShareMem,
      SysUtils,
      ADODB,
      ExtCtrls,
      Classes;{$R *.res}
    ///
    function AddTch(ADOConnection:TADOConnection;CNum:string):boolean;export;
    var
      ADOQuery:TADOQuery;
    begin
      ADOQuery := TADOQuery.Create(ADOConnection);
      if not ADOConnection.Connected then
        ADOConnection.Open;
      ADOQuery.Connection := ADOConnection;
      try
        with ADOQuery do
        begin
          Close;
          SQL.Clear;
          SQL.Text := 'Select * from TchInfo';
          Open;
          Append;
          FieldByName('CNum').AsString := CNum;
          Post;
          AddTch := true;
        end;
      except
        AddTch := false;
      end;
      ADOQuery.Free;
    end;end;
    ///
    exports
      AddTch;
    begin
    end.
    --------------------------------------------------
    下面是工程中调用
    ----------------
    function AddTch(ADOConnection:TADOConnection;CNum:string):boolean;Far;External TeacherInfo.dll';//在程序开始加上声明调用
    if AddTch(ADOConnection1,'123') = true then//程序中使用
      showmessage('添加信息成功');
      

  4.   

    'TeacherInfo.dll';//在程序开始加上声明
    --------------
    刚才这里少了个引号