procedure TDM.CreateMDB(DBName: string); // 系统需要安装 MADAC
var
CreateAccess: OleVariant;
begin
CreateAccess := CreateOleObject('ADOX.Catalog');
CreateAccess.Create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + DBName);
end;
procedure TDM.DataModuleCreate(Sender: TObject);
procedure ExtractStr(const ConStr: string);
var
s: string;
mPos: Integer;
begin
s := Constr + ';';
with TStringlist.Create do
begin
try
while Pos(';', s) > 0 do
begin
mPos := Pos(';', s);
Add(Copy(s, 1, mPos - 1));
System.Delete(s, 1, mPos);
end;
FMacineName := Values['Data Source'];
FDBName := Values['Initial Catalog'];
finally
Free;
end;
end;
end;
function DBExists: Boolean;
begin
with ads_Pub do
begin
Close;
CommandText := 'SELECT * FROM sysdatabases WHERE name = ''' + FDBName + '''';
Open;
if isEmpty then
Result := False
else
Result := True;
end;
end;
function CreateSQLServerDB: Boolean;
begin
cmd_Pub.CommandText := 'Create database ' + FDBName;
{fanghe on ' +
'( Name = fanghe_data, FileName = ''c:/test.mdf'',' +
'size = 15, MaxSize = 20, FileGrowth = 5 ) ' +
'Log on ( Name = fanghe_log, FileName = ''c:/test.1df'',' +
'Size = 3, MaxSize = 10, FileGrowth = 1 )';}
try
cmd_Pub.Execute;
Result := True;
except
Result := False;
end;
end;
var
TableSt: TStrings;
sfile: string;
begin
SCreen.Cursor := crHourGlass;
try
TableSt := TStringList.Create;
Sfile := CurDir + 'Datapath';
isServer := False;
if FileExists(sfile) then
begin
TableSt.LoadFromFile(Sfile);
ExtractStr(TableSt.Text);
ADOConn.ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;' +
'User ID=sa;Initial Catalog=master;Data Source=' + FMacineName;
ADOConn.Connected := True;
if not DBExists then
if not CreateSQLServerDB then
begin
Application.MessageBox('数据库创建失败!', '系统信息', 0);
Exit;
end;
ADOConn.Connected := False;
ADOConn.ConnectionString := TableSt.Text;
isServer := True;
end
else
begin
if not DirectoryExists(CurDir + 'Data') then
ForceDirectories(CurDir + 'Data');
Sfile := CurDir + 'Data/Account.mdb';
if not FileExists(Sfile) then
begin
try
CreateMDB(Sfile);
except
Application.MessageBox('数据库创建失败!', '系统信息', 0);
Exit;
end;
ADOConn.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;' +
'Data Source=./Data/Account.mdb;Mode=Share Deny None;Extended Properties="";' +
'Jet OLEDB:System database="";Jet OLEDB:Registry Path="";' +
'Jet OLEDB
atabase Password="";Jet OLEDB:Engine Type=5;' +
'Jet OLEDB
atabase Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;' +
'Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";' +
'Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;' +
'Jet OLEDB
on''t Copy Locale on Compact=False;' +
'Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False';
end;
end;
try
ADOConn.Connected := True;
except
Application.MessageBox('数据库连接失败!', '系统信息', 0);
Exit;
end;
TableSt.Clear;
try
ADOConn.GetTableNames(TableSt);
if TableSt.IndexOf('GlobeParam') = -1 then
SQLBat.RunScript;
finally
TableSt.Free;
end;
finally
Screen.Cursor := crDefault;
end;
end;