delphi6+SQLServer7.0,用TDatabase实现,应该对你有用:
unit InitDbDM;
interface
uses
SysUtils, Classes, DB, DBTables, Registry, Windows, TlHelp32;
type
TInitDbDataMoudule = class(TDataModule)
dbInitDb: TDatabase;
private
{ Private declarations }
function GetSQLServerPath:String;
public
{ Public declarations }
function IsSQLServerRunning:Boolean;
procedure CreateDb(dbName : String;
dbSize : String);
end;
var
InitDbDataMoudule: TInitDbDataMoudule;
implementation
{$R *.dfm}
//**************************************************
// 功能:取得SQLServer的安装路径
//
// 返回:SQLServer的安装路径
//**************************************************
function TInitDbDataMoudule.GetSQLServerPath:String;
var
reg : TRegistry;
path : String;
begin
path := '';
reg :=TRegistry.Create;
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.OpenKey('Software/Microsoft/MSSQLServer/Setup', False);
path := reg.ReadString('SqlPath');
reg.CloseKey;
reg.free;
Result := path;
end;
//**************************************************
// 功能:检测SQLServer是否正在运行
//
// 返回:true 正在运行
// false 不在运行
//**************************************************
function TInitDbDataMoudule.IsSQLServerRunning: Boolean;
var
finded : bool;
proListHandle : THandle;
proStruct : TProcessentry32; //记录进程的数据结构;
begin
Result := False;
proListHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
proStruct.dwSize := SizeOf(proStruct);
finded := Process32First(proListHandle, proStruct);//发现第一个进程;
while integer(finded) <> 0 do
begin
if Pos('SQLSERVR.EXE', Uppercase(proStruct.szExeFile)) <> 0 then
begin
Result:=True;
Break;
end;
finded := Process32Next(proListHandle, proStruct);//发现下一个进程;
end;
end;
//**************************************************
// 功能:创建SQLServer数据库
//
// 参数:dbName 数据库名
// dbSize 数据库大小
//**************************************************
procedure TInitDbDataMoudule.CreateDb(dbName : String;
dbSize : String);
var
sqlPath : String;
dataFile : String;
logFile : String;
createDbSql : String;
begin
sqlPath := GetSQLServerPath;
dataFile := sqlPath + '/data/' + dbName + '_data.mdf';
logFile := sqlPath + '/data/' + dbName + '_log.ldf';
createDbSql := 'Create Database ' + dbName +
' on primary(Name = ' + dbName + '_dat' +
', FileName = "' + dataFile +
'" , Size = ' + dbSize +
', FileGrowth = 1) ' +
'log on (Name = ' + dbName + '_log' +
', FileName = "' + logFile +
'", Size = ' + dbSize +
', FileGrowth = 1)';
if Not (FileExists(dataFile) or FileExists(logFile)) then
dbInitDb.Execute(createDbSql);
end;
end.