procedure TFrmMain.Button1Click(Sender: TObject);
var
reg: TRegistry;
strPath :String;
strdata,strdata1,strlog:String;
ComputerName:array[0..199] of char;
Size
word;
StrComputername:String;
begin
reg :=Tregistry.Create;
reg.RootKey := HKEY_LOCAL_MACHINE;
if reg.KeyExists('Software/Microsoft/MSSQLServer/Setup')=false then
begin
showmessage('请在使用本软件前安装sql server');
exit;
end;
reg.OpenKey('Software/Microsoft/MSSQLServer/Setup',False);
strPath :=reg.ReadString('SQLPath');
// strDPath :=reg.ReadString('SQLDataRoot');
// showmessage(strPath);
strdata:=strPath+'/data';
// showmessage(strdata); //所查找到的data目录
//showmessage(strDpath);
reg.CloseKey;
reg.free;
strdata1:=strdata+'/community.mdf';
strlog:=strdata+'/community.ldf';
// 以下要调用一个方法来实现copy data到当前sql server 的data目录下面。
if not Copyfile(Pchar(ExtractFilepath(paramstr(0))+'/data/community.mdf'),pchar(strdata+'/community.mdf'),false)
then
begin
Messagebox(handle,'拷贝数据库时候出现错误,即将退出','错误',MB_OK+MB_ICONERROR);
Exit;
end;
if not Copyfile(Pchar(ExtractFilepath(paramstr(0))+'/data/community.ldf'),pchar(strdata+'/community.ldf'),false)
then
begin
Messagebox(handle,'拷贝数据库时候出现错误,即将退出','错误',MB_OK+MB_ICONERROR);
Exit;
end;
size:=199;
if GetComputerName(Computername,size) then
begin
StrComputerName:=StrPas(ComputerName);
//showmessage(strcomputerName);
end
else
begin
MessageBox(handle,'可能由于该计算机名字过长等原因,未能得到该计算机的名字,即将退出','错误',Mb_OK+MB_ICONERROR);
Exit;
end;
//开始设置connectionstring
adoquery1.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source='+strComputerName;
with adoquery1 do
begin
sql.Clear;
sql.Add('sp_attach_db ''community'',');
// sql.add(' N''');
sql.add(''''+strdata1+'''');
sql.add(',');
// sql.add( 'N''');
sql.add(''''+strlog+'''');
execsql;
showmessage('已经安装成功');
end;