本人一直用的是c/c++,但是现在有一门delphi的选修课,要求交一个连接数据库的小程序。
最近大四一直忙着找工作,实在没时间顾这个,现在快要交了。哪位朋友帮帮忙,寄一个到我的信箱吧。数据库就用access的,不要太大,也不要太复杂,要能调试通过的。
请发到我的信箱:[email protected]

解决方案 »

  1.   

    用ADO连数据库,放一个AdoConnection,双击它,点弹出的对话框的Bulid按钮,你就知道怎么连了
      

  2.   

    在楼上的基础上选择第二项
    找到你要的access数据库
    就可以了!
      

  3.   

    用TAdoConnection就可以了,很简单的。
      

  4.   

    ADOConnection1.close;
       ADOConnection1.ConnectionString :='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='''+g_path+''';Persist Security Info=False';
       ADOConnection1.Open ;
       ADOConnection1.Connected :=true; 
    其中g_path是你的数据库文件的路径,ok
      

  5.   

    {项目名称:
    单元名称: USourceInfo
    单元说明:
    作    者:Li ShaoLong
    创建日期:2003-10-31
    最终修正日期:
    版    本:v0.5History :
      修改日期:
      修改人员:
      修改版本:
      修改说明:********************************************************************************}
    unit USourceInfo;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,registry, DB, ADODB, Buttons;type
      TfrmSourceInfo = class(TForm)
        btnOK: TButton;
        btnCancel: TButton;
        ADOConnection: TADOConnection;
        GroupBox1: TGroupBox;
        labDataSource: TLabel;
        labUserName: TLabel;
        labPassWord: TLabel;
        edtPassWord: TEdit;
        edtUserName: TEdit;
        edtDataSource: TEdit;
        btnConnect: TButton;
        opends: TSpeedButton;
        OD1: TOpenDialog;
        procedure btnOKClick(Sender: TObject);
        procedure btnCancelClick(Sender: TObject);
        procedure btnConnectClick(Sender: TObject);
        procedure opendsClick(Sender: TObject);
      private
         function CryptStr(strSource:String; Stype: Dword):String;
      public
        { Public declarations }
      end;//定义类,用来对数据源进行操作
      TSourceInfo=class    private //------------------ >私有变量或过程函数
          FDataSource:string; //----- >用来存放数据源字符串
          FUserName:string;   //----- >用来存放用户名
          FPassWord:string;   //----- >用来存放用户密码
          procedure Init;     //----- >初始负值
          procedure RegLinkInfo; //--- >信息写入注册表
    //    procedure SetDBUserID;
    //    procedure SetDBPassWord;    public//------------------- >公有函数或过程
      //取得数据源字符串
          function GetDataSource:string;
      //取得连接数据源所用的用户名
          function GetDBUserID:string;
      //取得连接数据源所用的用户密码
          function GetDBPassWord:string;
    //    property PDataSource:string read FDataSource write FDataSource;
    //    property PUserName:string read FUserName write FUserName;
    //    property PFPassWord:string read FPassWord write FPassWord;   end;//-------------------- >类定义结束var
      frmSourceInfo: TfrmSourceInfo;
      GetSource:TSourceInfo;
      adoconn:tadoconnection;
    implementation{$R *.dfm}function TfrmSourceInfo.CryptStr(strSource:String; Stype: Dword):String;
    var
      i: integer;
      Fkey: integer;
    begin
      Result:='';
      Case Stype of
        0: //加密
          begin
            for i:=1 to length(strSource) do
              Result := Result+chr( ord(strSource[i]) xor i);
          end;
        1: //解密
         begin
            Fkey := Ord(strSource[length(strSource)]);
            for i:=1 to length(strSource) do
              Result := Result+chr( ord(strSource[i]) xor i);
         end;
      end;  //case endend;procedure TSourceInfo.Init;
    begin
      FDataSource := frmSourceInfo.edtDataSource.Text;
      FUserName   := frmSourceInfo.edtUserName.Text  ;
      FPassWord   := frmSourceInfo.edtPassWord.Text  ;
    end;function TSourceInfo.GetDataSource:string;
    var
      Reg: TRegistry;
      S: string;
    begin
       //读取注册表
       Reg := TRegistry.Create;
       Reg.RootKey := HKEY_CURRENT_USER;
       Reg.OpenKey('\SoftWare\MyWife', true);
       S := Reg.ReadString ('DataSource');
    //   showmessage(s);
       Reg.CloseKey;  //关闭
       Reg.Free;      //释放
       Result := S;
    end;function TSourceInfo.GetDBUserID:string;
    var
      Reg: TRegistry;
      S: string;
    begin
       //读取注册表
       Reg := TRegistry.Create;
       Reg.RootKey := HKEY_CURRENT_USER;
       Reg.OpenKey('\SoftWare\MyWife', true);
       S := Reg.ReadString ('UserName');
       Reg.CloseKey;  //关闭
       Reg.Free;      //释放
       Result := S;
    end;function TSourceInfo.GetDBPassWord:string;
    var
      Reg: TRegistry;
      S: string;
    begin
       //读取注册表
       Reg := TRegistry.Create;
       Reg.RootKey := HKEY_CURRENT_USER;
       Reg.OpenKey('\SoftWare\MyWife', true);
       S := Reg.ReadString ('PassWord');
       Reg.CloseKey;  //关闭
       Reg.Free;      //释放
       Result := S;
    end;procedure TSourceInfo.RegLinkInfo ;
    var
      Reg: TRegistry;
    begin
       GetSource:=TSourceInfo.Create ;
       GetSource.Init ;
       //修改注册表
       Reg := TRegistry.Create;
       Reg.RootKey := HKEY_CURRENT_USER;
       Reg.OpenKey('\SoftWare\MyWife', true);
       Reg.WriteString('DataSource', extractfilepath(application.ExeName)+GetSource.FDataSource);
       Reg.WriteString('UserName', GetSource.FUserName);
       Reg.WriteString('PassWord', GetSource.FPassWord);
       Reg.CloseKey;  //关闭
       Reg.Free;      //释放
    end;procedure TfrmSourceInfo.btnOKClick(Sender: TObject);
    begin
      GetSource.RegLinkInfo ;
      Close;
    end;procedure TfrmSourceInfo.btnCancelClick(Sender: TObject);
    begin
      close;
    end;procedure TfrmSourceInfo.btnConnectClick(Sender: TObject);
    var
      strAccess: string;
    begin
       GetSource:=TSourceInfo.Create ;
       GetSource.Init ;
      if Trim(edtDataSource.Text) = '' then
      begin
        MessageBox(Application.Handle,'数据源不能为空','提示',MB_OK+MB_IconInformation);
        Abort;
      end;
      if Trim(edtUserName.Text) = '' then
      begin
        MessageBox(Application.Handle,'用户名不能为空','提示',MB_OK+MB_IconInformation);
        Abort;
      end;  strAccess :=
                'Provider=Microsoft.Jet.OLEDB.4.0'+
                ';Data Source='+GetSource.FDataSource+
                ';Persist Security Info=False'+
                ';Jet OLEDB:Database Password='+GetSource.FPassWord+
                ';User ID='+GetSource.FUserName;  try
        ADOConnection.Connected := False;
        ADOConnection.ConnectionString := strAccess;
        ADOConnection.LoginPrompt := False;
        ADOConnection.Connected := True;
        MessageBox(Application.Handle,'连接成功!','提示',MB_OK+MB_IconInformation);
      except
        MessageBox(Application.Handle,'连接失败!','提示',MB_OK+MB_IconInformation);
        Abort;
      end;
        end;procedure TfrmSourceInfo.opendsClick(Sender: TObject);
    begin
    if od1.Execute then
      edtDataSource.Text:=od1.FileName ;
    end;end.通用
      

  6.   

    ADOConnection1.close;
    ADOConnection1.ConnectionString :='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+dbfile+';Persist Security Info=False';
    ADOConnection1.Open ;只要用dbfile指定Access数据文件路径就OK了。
    关闭时只用
    ADOConnection1.Close ;