我的MSN是:[email protected]干吗没有人回答呢?

解决方案 »

  1.   

    我的msn :[email protected]
    有时间加我了。
      

  2.   

    举便来讲
    有一SQL数据库(GISJD),库中有一表(userName),结构为:
    用户名(strName),真实姓名(strXM)。我想做一个类,通过调用类的方法,实现知道用户名时查询真实姓名当然,目的并不是想实现这一点点功能
    而是以这个简单的例子
    熟悉Delphi对类的操作,了解类制作的方法请诸位大哥不吝指教
      

  3.   

    这是我做过的一个字典查询类,你新建一个单元(记住是单元哦,不是窗体),就可以写类了
    以下是原码,可以参考一下:
    unit WordsUnit;interfaceuses DataUnit,SysUtils,Classes,DB;Const
      constr='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False';type  TWord=Record
        WordName:string[50];//单词
        WordVoice:string[50];//音标
        WordExplain:string[100];//解释
      end;  TWhatDic=(cet4,cet6,Newconcept,Master,computer);
    //--------------------------------------
      TWordsCtrl = class(TObject)
      private
        WordInfo:TWord;
        Dic:string;
        //isFind:Boolean;//标识是否查到了单词
        //WhatDic:TWhatDic;//标识是哪一本字典
      public
    //初始化数据库连接
        procedure IniDBCon;
        procedure IniTableCon(WCon:TWhatDic);
    //精确查询,返回是否查得到
        Function SearchSure(WName:String):Boolean;
    //模糊查询,返回是否查得到
        Function SearchBlur(WName:String):Boolean;
    //返回查到的单词记录,前提是必须使用过上面两个函数才能用
        Function ReturnWords:TWord;
    //大范围查询,列出两个函数
        Function SearchBlurFirst(WName:string):Boolean;
        Function SearchBlurNext:Boolean;
    //增加单词,增加成功返回true增加失败,返回false
         Function AddWords(Name,Voice,explain:string):Boolean;
    //将新单词本列出来的两个函数
    //如果第个成功,即新单词本中有单词,则可调用下一下函数
    //没有,则返回false,第二个函数一样
        Function NewFirstWord:Boolean;
        Function NewNextWord:Boolean;
      end;
    //-------------------------------------------implementationprocedure TWordsCtrl.IniDBCon;
    var ConFilePath:String;
    begin
      ConFilePath:=ExtractFilePath(Paramstr(0))+'\WordData\Words.mdb';
      DataModule1.ADOConnection1.ConnectionString:=Format(constr,[conFilePath]);
    end;Procedure TWordsCtrl.IniTableCon(WCon:TWhatDic);
    begin
      DataModule1.ADOQuery1.Close;
      DataModule1.ADOQuery1.SQL.Clear;
      if Wcon=cet4 then
      begin
        DataModule1.ADOQuery1.SQL.Add('Select* from 大学四级');
        Dic:='大学四级';
      end;
      if Wcon=cet6 then
      begin
        DataModule1.ADOQuery1.SQL.Add('Select* from 大学六级');
        Dic:='大学六级';
      end;
      if Wcon=Newconcept then
      begin
        DataModule1.ADOQuery1.SQL.Add('Select* from 新概念');
        Dic:='新概念';
      end;
      if Wcon=Master then
      begin
        DataModule1.ADOQuery1.SQL.Add('Select* from 研究生');
        Dic:='研究生';
      end;
      if Wcon=Computer then
      begin
        DataModule1.ADOQuery1.SQL.Add('Select* from 计算机');
        Dic:='计算机';
      end;
      DataModule1.ADOQuery1.Open;
    end;Function TWordsCtrl.SearchSure(WName:String):Boolean;
    begin
      Result:=False;
      DataModule1.ADOQuery1.Open;
      if DataModule1.ADOQuery1.Locate('单词',WName,[loCaseInsensitive])then
      begin
        WordInfo.WordName:=DataModule1.ADOQuery1.FieldValues['单词'];
        WordInfo.WordVoice:=DataModule1.ADOQuery1.FieldValues['音标'];
        WordInfo.WordExplain:=DataModule1.ADOQuery1.FieldValues['解释'];
        Result:=True;
      end;
    end;Function TWordsCtrl.SearchBlur(WName:String):Boolean;
    begin
      Result:=False;
      DataModule1.ADOQuery1.Open;
      if DataModule1.ADOQuery1.Locate('单词',WName,[loPartialKey])then
      begin
        WordInfo.WordName:=DataModule1.ADOQuery1.FieldValues['单词'];
        WordInfo.WordVoice:=DataModule1.ADOQuery1.FieldValues['音标'];
        WordInfo.WordExplain:=DataModule1.ADOQuery1.FieldValues['解释'];
        Result:=True;
      end;
    end;Function TWordsCtrl.ReturnWords:TWord;
    begin
       Result:=WordInfo;
    end;Function TWordsCtrl.SearchBlurFirst( Wname:string):Boolean;
    begin
       DataModule1.ADOQuery1.Close;
       DataModule1.ADOQuery1.SQL.Clear;
       DataModule1.ADOQuery1.SQL.add('select top 20 * from '+Dic+' where 单词 like ''%'+Wname+'%''');
       DataModule1.ADOQuery1.Open;
       if not DataModule1.ADOQuery1.Eof then
       begin
       WordInfo.WordName:=DataModule1.ADOQuery1.FieldValues['单词'];
       WordInfo.WordVoice:=DataModule1.ADOQuery1.FieldValues['音标'];
       WordInfo.WordExplain:=DataModule1.ADOQuery1.FieldValues['解释'];
       Result:=True;
       end
       else Result:=False;
    end;Function TWordsCtrl.SearchBlurNext:Boolean;
    begin
         DataModule1.ADOQuery1.Next;
       if not DataModule1.ADOQuery1.Eof then
       begin
          WordInfo.WordName:=DataModule1.ADOQuery1.FieldValues['单词'];
          WordInfo.WordVoice:=DataModule1.ADOQuery1.FieldValues['音标'];
          WordInfo.WordExplain:=DataModule1.ADOQuery1.FieldValues['解释'];
          Result:=True;
       end
       else
          Result:=False;
    end;Function TWordsCtrl.AddWords(Name,Voice,explain:string):Boolean;
    begin
      DataModule1.ADOQuery1.Close;
      DataModule1.ADOQuery1.SQL.Clear;
      DataModule1.ADOQuery1.SQL.Add('select * from 新词汇');
      DataModule1.ADOQuery1.Open;
      DataModule1.ADOQuery1.Edit;
      DataModule1.ADOQuery1.Last;
      DataModule1.ADOQuery1.Insert;
      DataModule1.ADOQuery1.FieldByName('单词').AsString:=Name;
      DataModule1.ADOQuery1.FieldByName('音标').AsString:=Voice;
      DataModule1.ADOQuery1.FieldByName('解释').AsString:=Explain;
      DataModule1.ADOQuery1.Post;
      DataModule1.ADOQuery1.Close;
      DataModule1.ADOQuery1.SQL.Clear;
      DataModule1.ADOQuery1.SQL.Add('select * from '+dic);
      DataModule1.ADOQuery1.Open;
      Result:=true;
    end;Function TWordsCtrl.NewFirstWord:Boolean;
    begin
      DataModule1.ADOQuery1.Close;
      DataModule1.ADOQuery1.SQL.Clear;
      DataModule1.ADOQuery1.SQL.Add('select * from 新词汇');
      DataModule1.ADOQuery1.Open;
      if not DataModule1.ADOQuery1.Eof then
      begin
        WordInfo.WordName:=DataModule1.ADOQuery1.FieldValues['单词'];
        WordInfo.WordVoice:=DataModule1.ADOQuery1.FieldValues['音标'];
        WordInfo.WordExplain:=DataModule1.ADOQuery1.FieldValues['解释'];
        Result:=True;
      end
      else
        Result:=False;
    end;Function TWordsCtrl.NewNextWord:Boolean;
    begin
      DataModule1.ADOQuery1.Next;
      if not DataModule1.ADOQuery1.Eof then
      begin
        WordInfo.WordName:=DataModule1.ADOQuery1.FieldValues['单词'];
        WordInfo.WordVoice:=DataModule1.ADOQuery1.FieldValues['音标'];
        WordInfo.WordExplain:=DataModule1.ADOQuery1.FieldValues['解释'];
        Result:=True;
      end
      else
        Result:=False;
    end;end.
      

  4.   

    谢谢 linzhengqun(山之峰) 
    看了你的程序,深受启发不过为了节约时间,我将继类定为微软的Tadoquery
    已基于实现了如上功能(传入查询表的查询字段及限制条件,输出查询内容)
    程序如下:unit adoquerym;interfaceuses
      SysUtils, Classes, DB, ADODB;type
      tadoquerym = class(TADOQuery)
      private
        { Private declarations }
      protected
        { Protected declarations }
      public
        { Public declarations }
         function getValue(strTableName,strFieldTj,strTj,strFieldCx:string;conn:TADOConnection):string;
      published
        { Published declarations }
      end;procedure Register;var
        queryTemp:tadoquerym;implementationprocedure Register;
    begin
      RegisterComponents('mytools', [tadoquerym]);
    end;//函数功能:根据传入表(strTableName)传入字段(strFieldTj)及传入字段限制值(strTj)
    //         取查询字段的值(strFieldCx),如没有对应值则返回空,如有多个值由返回第一个
    function tadoquerym.getValue(strTableName,strFieldTj,strTj,strFieldCx:string;conn:TADOConnection):string;
    begin
      try
        queryTemp:=tadoquerym.Create(Self);
        queryTemp.Connection:=conn;
        queryTemp.Active:=false;
        queryTemp.SQL.Clear;
        queryTemp.SQL.Text:='select '+strFieldCx+' from '+strTableName+' where '+strFieldTj+'='''+strTj+'''' ;
        queryTemp.Active:=true;
        if queryTemp.RecordCount<1 then
          getValue:=''
        else
          getValue:=trim(queryTemp.Fields[0].AsString);
      except
        getValue:='';
      end;
    end;end.可是遇到了一个很奇怪的问题
    在新程序中加入此控件后,明明设置了它的conn属性,可是新建一个实例后,进行程序却提示
    没有连接(connection)是不是我没有构造函数和析构函数
      

  5.   

    {**************************************************************************** WLDB.pas 封装ADO的ADOConnection, ADOQuery, ADOTable和DataSource
     实现了数据库链接, 数据库操作.
     赖国荣 (WinLai)
     [email protected]
     2004-4-5----------------------------------------------------------------------------------------------------------------------------------------------------------****************************************************************************}
    unit WLDB;interfaceuses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, ADODB, DB,
    DBGrids;{============================================================================
    TWLConnection Class
    ============================================================================}
    type
    TWLConnection = class(TADOConnection)
    private protected public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override; function Connect(const FileName: string; const Password: string=''): Boolean;
    procedure Disconnect;
    function CheckConnect: boolean; publishedend;{============================================================================
    TWLTable Class
    ============================================================================}
    type
    TWLTable = class(TObject)
    private
    FTableName: string;
    FRecNo: Integer;
    FConnection: TADOConnection; FADOQuery: TADOQuery;
    FADOTable: TADOTable;
    FDataSource: TDataSource; protected
    procedure SetTableName(const Value: string);
    procedure SetRecNo(const Value: Integer);
    function GetRecordCount: Integer;
    function GetRecNo: Integer;
    function GetConnection: TADOConnection;
    procedure SetConnection(const Value: TADOConnection); public
    constructor Create(AOwner: TComponent); virtual;
    destructor Destroy; override; function SQLExec(const Value: string): Boolean;
    function SQLOpen(const Value: string): Boolean;
    procedure SetDataSourceToTable(DataControl: TDBGrid); property ADOQuery: TADOQuery read FADOQuery write FADOQuery;
    property ADOTable: TADOTable read FADOTable write FADOTable;
    property DataSource: TDataSource read FDataSource write FDataSource;
    property TableName: string read FTableName write SetTableName;
    property RecordCount: Integer read GetRecordCount;
    property RecNo: Integer read GetRecNo write SetRecNo;
    property Connection: TADOConnection read GetConnection write SetConnection; publishedend;implementation{ TWLConnection Class }
    //---------------------------------------------------------------------------
    constructor TWLConnection.Create(AOwner: TComponent);
    begin
    inherited Create(nil); LoginPrompt := false;
    ConnectionString := '';
    end;//---------------------------------------------------------------------------
    destructor TWLConnection.Destroy;
    begin
    Disconnect; inherited Destroy;
    end;//---------------------------------------------------------------------------
    function TWLConnection.Connect(const FileName: string; const Password: string=''): Boolean;
    begin
    try
    Result := false; Close;
    ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password='+Password+';Data Source=' + FileName;
    Open; if CheckConnect=true then
    begin
    Result := true;
    end;
    except
    Result := false;
    end;
    end;//---------------------------------------------------------------------------
    procedure TWLConnection.Disconnect;
    begin
    Close;
    end;//---------------------------------------------------------------------------
    function TWLConnection.CheckConnect: boolean;
    begin
    Result := false;
    if Connected=true then
    begin
    Result := true;
    end;
    end;{ TWLTable Class }
    //---------------------------------------------------------------------------
    constructor TWLTable.Create(AOwner: TComponent);
    begin
    inherited Create(); FADOQuery := TADOQuery.Create(nil);
    FADOTable := TADOTable.Create(nil);
    FDataSource := TDataSource.Create(nil); FTableName := '';
    end;//---------------------------------------------------------------------------
    destructor TWLTable.Destroy;
    begin
    FADOQuery.Free;
    FADOTable.Free;
    FDataSource.Free; inherited Destroy;
    end;//---------------------------------------------------------------------------
    procedure TWLTable.SetTableName(const Value: string);
    begin
    FTableName := Value;
    end;//---------------------------------------------------------------------------
    procedure TWLTable.SetRecNo(const Value: Integer);
    begin
    ADOQuery.RecNo := Value;
    FRecNo := Value;
    end;//---------------------------------------------------------------------------
    function TWLTable.GetRecordCount: Integer;
    begin
    Result := ADOQuery.RecordCount;
    end;//---------------------------------------------------------------------------
    function TWLTable.GetRecNo: Integer;
    begin
    FRecNo := ADOQuery.RecNo;
    Result := FRecNo;
    end;//---------------------------------------------------------------------------
    function TWLTable.GetConnection: TADOConnection;
    begin
    Result := FConnection;
    end;//---------------------------------------------------------------------------
    procedure TWLTable.SetConnection(const Value: TADOConnection);
    begin
    FADOQuery.Connection := Value;
    FADOTable.Connection := Value;
    FConnection := Value;
    end;//---------------------------------------------------------------------------
    function TWLTable.SQLExec(const Value: string): boolean;
    begin
    try
    FADOQuery.Close;
    FADOQuery.SQL.Text := Value;
    FADOQuery.ExecSQL; Result := true;
    except
    Result := false;
    end;
    end;//---------------------------------------------------------------------------
    function TWLTable.SQLOpen(const Value: string): boolean;
    begin
    try
    FADOQuery.Close;
    FADOQuery.SQL.Text := Value;
    FADOQuery.Open; Result := true;
    except
    Result := false;
    end;
    end;//---------------------------------------------------------------------------
    procedure TWLTable.SetDataSourceToTable(DataControl: TDBGrid);
    begin
    FDataSource.DataSet := FADOTable;
    DataControl.DataSource := FDataSource;
    end;//---------------------------------------------------------------------------
    end.
      

  6.   

    构造函数和析构函数在Delphi中是不可少的!你可以加上试试,不过,如果说是没有连接,好像不是构造函数和析构函数的问题,你可不可以把错误提示完整的写下来!
      

  7.   

    {**************************************************************************** WLTable.pas 赖国荣 (WinLai)
     [email protected]
     2004-4-12----------------------------------------------------------------------------------------------------------------------------------------------------------****************************************************************************}
    unit WLTable;interface
    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, ADODB, DB,
    WLDB, CommonData, Dialogs;type
    {============================================================================
    TWLEmployee Class
    ============================================================================}
    TWLEmployee = class(TWLTable)
    private protected public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override; property TableName; function AppendRecord(const Value: array of string): Boolean;
    function BrowseRecord: Boolean;
    function DeleteRecord(const C1, V1: string): Boolean;
    function EditRecord(const Value: array of string; const C1, V1: string): Boolean;
    function FindRecord(const C1, V1: string): Boolean; published end;implementation { TWLEmployee Class}
    //---------------------------------------------------------------------------
    constructor TWLEmployee.Create(AOwner: TComponent);
    begin
    inherited Create(nil); TableName := 'Employee';
    end;//---------------------------------------------------------------------------
    destructor TWLEmployee.Destroy;
    begin
    //... inherited Destroy;
    end;//---------------------------------------------------------------------------
    function TWLEmployee.AppendRecord(const Value: array of string): Boolean;
    var
    SqlStr: string;
    begin
    SqlStr := 'insert into Employee(' +
    'sID, sObligorInforID, sName, sAddress, sPhone'+
    ') values(' +
    FormatValue(Value[0], dtText) + ',' +
    FormatValue(Value[1], dtText) + ',' +
    FormatValue(Value[2], dtText) + ',' +
    FormatValue(Value[3], dtText) + ',' +
    FormatValue(Value[4], dtText) + ')';
    Result := SQLExec(SqlStr);
    end;//---------------------------------------------------------------------------
    function TWLEmployee.BrowseRecord: Boolean;
    var
    SqlStr: string;
    begin
    SqlStr := 'select * from Employee';
    Result := SQLOpen(SqlStr);
    if RecordCount=0 then
    Result := false;
    end;//---------------------------------------------------------------------------
    function TWLEmployee.DeleteRecord(const C1, V1: string): Boolean;
    var
    SqlStr: string;
    begin
    SqlStr := 'delete from Employee where '+C1+'='+V1;
    Result := SQLExec(SqlStr);
    end;//---------------------------------------------------------------------------
    function TWLEmployee.EditRecord(const Value: array of string; const C1, V1: string): Boolean;
    var
    SqlStr: string;
    begin
    SqlStr := 'update Employee set ' +
    'sID=' +FormatValue(Value[0], dtText)+','+
    'sObligorInforID='+FormatValue(Value[1], dtText)+','+
    'sName=' +FormatValue(Value[2], dtText)+','+
    'sAddress=' +FormatValue(Value[3], dtText)+','+
    'sPhone=' +FormatValue(Value[4], dtText)+' '+
    'where '+C1+'='+V1;
    Result := SQLExec(SqlStr);
    end;//---------------------------------------------------------------------------
    function TWLEmployee.FindRecord(const C1, V1: string): Boolean;
    var
    SqlStr: string;
    begin
    SqlStr := 'select * from Employee where '+C1+'='+V1;
    Result := SQLOpen(SqlStr);
    if RecordCount=0 then
    Result := false;
    end;//---------------------------------------------------------------------------
    end.
      

  8.   

    谢谢大家to:tsst(田sir) 
    我将此程序编译好后,在其他程序中加入此控件
    明明设置了他的connection属性
    可运行时却提示"missing connection or connectionstring"
    我敢肯定这没有设置连接字符串所致所以我就用了一个折中的办法
    在自定义函数中,新增一参数,将连接字符串传入
        function getValue(strTableName,strFieldTj,strTj,strFieldCx:string;conn:TADOConnection):string;
    然后在自定义函数中将其连接属性设为conn
        queryTemp.Connection:=conn;我想是不是因为我的queryTemp是新定义的,
    var
        queryTemp:tadoquerym;。 queryTemp:=tadoquerym.Create(Self);
    可这是不办法
    这样的类也太丢人了
      

  9.   

    如果不出我所料
    真的是这样原因(自作多情在类设计中定义了一个类的实例),然后用这个实例来操作数据库
    var
        queryTemp:tadoquerym;。 queryTemp:=tadoquerym.Create(Self);谢谢 laiguorong(Win Lai) 看了 laiguorong(Win Lai) 的程序后才明白
    在类定义中可以直接操作类虽不是特别好的一个程序
    但毕竟是我写的第一个类
    为方便其他朋友,不敢独享
    将改正后的程序贴上来
      

  10.   

    unit adoquerym;interfaceuses
      SysUtils, Classes, DB, ADODB;type
      tadoquerym = class(TADOQuery)
      private
        { Private declarations }
      protected
        { Protected declarations }
      public
        { Public declarations }
         function getValue(strTableName,strFieldTj,strTj,strFieldCx:string):string;
      published
        { Published declarations }
      end;procedure Register;
    implementationprocedure Register;
    begin
      RegisterComponents('mytools', [tadoquerym]);
    end;//函数功能:根据传入表(strTableName)传入字段(strFieldTj)及传入字段限制值(strTj)
    //         取查询字段的值(strFieldCx),如没有对应值则返回空,如有多个值由返回第一个
    function tadoquerym.getValue(strTableName,strFieldTj,strTj,strFieldCx:string):string;
    begin
      try
        Active:=false;
        SQL.Clear;
        SQL.Text:='select '+strFieldCx+' from '+strTableName+' where '+strFieldTj+'='''+strTj+'''' ;
        Active:=true;
        if RecordCount<1 then
          getValue:=''
        else
          getValue:=trim(Fields[0].AsString);
      except
        getValue:='';
      end;
    end;end.