分发软件时,如何直接把做好Sql 数据库附加到别人机子上Sql Server!!!!
以免还要在企业管理器重新附加....

解决方案 »

  1.   

    form2.ADOConnection1.Execute(' sp_attach_single_file_db @dbname = '+''''+'jjdb'+''''+', @physname = '+''''+label2.Caption+'\jjdb_Data.mdf'+'''');
      

  2.   

    //这可是具有商业价值的代码哟
    //被老板看见就惨了(看完赶紧删了)
    unit UnitMain;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Mask, DB, ADODB,Registry,comobj,activex,shlobj, jpeg,
      ExtCtrls;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Label1: TLabel;
        Label2: TLabel;
        Label3: TLabel;
        Button1: TButton;
        Button2: TButton;
        Label4: TLabel;
        MaskEdit1: TMaskEdit;
        ADOConnection1: TADOConnection;
        ADOQuery1: TADOQuery;
        ADOStoredProc1: TADOStoredProc;
        Image1: TImage;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
        procedure Button3Click(Sender: TObject);
      private
        function copyDataFile:boolean;
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      success:boolean;
    implementation{$R *.dfm}procedure CreateLinkFileForWygl;
    var 
      tmpObject : IUnknown;
      tmpSLink : IShellLink;
      tmpPFile : IPersistFile;
      PIDL : PItemIDList;
      StartupDirectory : array[0..MAX_PATH] of Char;
      StartupFilename : String;
      LinkFilename : WideString;
    begin
      StartupFilename := 'http://localhost/wyglWQB/default.aspx';
      tmpObject := CreateComObject(CLSID_ShellLink);//创建建立快捷方式的外壳扩展
      tmpSLink := tmpObject as IShellLink;//取得接口
      tmpPFile := tmpObject as IPersistFile;//用来储存*.lnk文件的接口
      tmpSLink.SetPath(pChar(StartupFilename));//设定notepad.exe所在路径
      tmpSLink.SetWorkingDirectory(pChar(ExtractFilePath(StartupFilename)));//设定工作目录
      SHGetSpecialFolderLocation(0,
      CSIDL_DESKTOPDIRECTORY,
      PIDL);//获得桌面的Itemidlist
      SHGetPathFromIDList(PIDL,
      StartupDirectory);//获得桌面路径
      LinkFilename := StartupDirectory + '\思睿物业管理系统.lnk';
      tmpPFile.Save(pWChar(LinkFilename),FALSE);//保存*.lnk文件
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var currentPath:string;
      Reg: TRegistry;
    begin
      Reg := TRegistry.Create;
      try
        //判断是否安装mssqlserver2000
        Reg.RootKey := HKEY_LOCAL_MACHINE;
        if not reg.openkeyReadOnly('\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion') then begin
          showmessage('MS SQL Server 2000不存在!请先安装MS SQL Server 2000。');
          exit;
        end;
        if not (reg.ReadString('CurrentVersion')>'8') then begin
          showmessage('MS SQL Server 版本太低!请先安装MS SQL Server 2000。');
          exit;
        end;
      finally
        reg.CloseKey;
        reg.Free;
      end;
      if not copyDataFile then begin//拷贝文件失败就退出安装
        application.MessageBox(pchar('数据库安装失败!'),pchar('错误'),mb_iconerror);
        exit;
      end;
      self.ADOConnection1.ConnectionString:= 'Provider=SQLOLEDB.1;Persist Security Info=False;Password='+maskedit1.text+';User ID='+self.Edit1.Text+';Data Source=localhost';
      currentPath:=extractFilePath(application.ExeName);
      with adoquery1 do
      begin
        SQL.Clear ;
        SQL.add('sp_attach_db @dbname=N'+''+'''wyglWQB'''+''+' , ');
        SQL.add('@filename1=N'+''''+'c:\wyglData\wyglWQB_Data.MDF'+''''+' , ');
        sql.add('@filename2=N'+''''+'c:\wyglData\wyglWQB_Log.LDF''')
      end;
      try
        try
          adoquery1.ExecSQL;
          showmessage('数据库安装成功!');
          CreateLinkFileForWygl;
          showmessage('物业管理系统安装完毕!请运行桌面上的“思睿物业管理系统”。');
          success:=true;
          close;
        except
          on E: Exception  do application.MessageBox(pchar('操作失败!失败原因如下:'+#13#10+e.Message),pchar('错误'),mb_iconError);
        end;
      finally
        adoconnection1.Close;
      end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
       close;
    end;procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    begin
      if success then begin
        canClose:=true;
        exit;
      end;
      if application.MessageBox(pchar('退出安装你将不能正常使用物业管理软件!'#13#10'你确定要退出吗?'),pchar('请确定'),MB_YESNO|mb_iconinformation)=IDYES  then
        canClose:=true
      else
        canClose:=false;
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
    CreateLinkFileForWygl;
    end;function TForm1.copyDataFile: boolean;
    var fullPath:string;
    begin
      if not DirectoryExists('c:\wyglData') then
        if not CreateDir('C:\wyglData') then begin
          application.MessageBox(pchar('不能创建文件夹 c:\wyglData'),pchar('数据库安装失败'),mb_iconerror);
          result:=false;
          exit;
        end;
      fullPath:=extractFilePath(application.ExeName);
      if not copyfile(pchar(fullPath+'wyglWQB_data.mdf'),pchar('c:\wyglData\wyglWQB_data.mdf'),true) then begin
         application.MessageBox(pchar('文件 c:\wyglData\wyglWQB_Data.mdf已经存在!'),pchar('错误失败'),mb_iconerror);
         result:=false;
      end else begin
        copyfile(pchar(fullPath+'wyglWQB_log.ldf'),pchar('c:\wyglData\wyglWQB_Log.ldf'),false);
        FileSetAttr('c:\wyglData\wyglWQB_data.mdf',32);//改变文件属性为可读写
        FileSetAttr('c:\wyglData\wyglWQB_log.ldf',32);
        result:=true;
      end;
    end;end.
      

  3.   

    我是这样做的,在打包时做一个附加数据库的程序(要求用户输入要添加数据库的计算机名或IP地址,SQL的管理员帐号),再调用附加数据库的存储过程。写好文挡,要用户在第一次运行主程序前先运行这个数据库初始化程序;
    这样就把数据库附加进去了