procedure TdmINP.DataModuleCreate(Sender: TObject);
const
SysDatabaseName = 'yourDB.mdb';
OdbcName = YOURODBC'; // ODBC数据源名称
var
RegOdbc: TRegistry; //存取ODBC数据源
dbParam: string; //数据库Params参数
WinSystemPath: array[0..40] of Char;
begin
//*********** 判断数据库是否存在 ************************//
if not FileExists(CurrPath + SysDatabaseName) then
begin
Application.MessageBox(PChar(Format('数据库文件%s在目录%s中不存在, 数据库没有打开!', [SysDatabaseName, CurrPath])), '系统告警', MB_ICONWARNING);
Application.Terminate;
end;
//*********** 判断ODBC数据源是否存在 ************************//
//建立ODBC
RegOdbc:= TRegistry.Create;
try
RegOdbc.RootKey:= HKEY_LOCAL_MACHINE;
RegOdbc.OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources', True);
RegOdbc.WriteString(OdbcName, 'Microsoft Access Driver (*.mdb)');
RegOdbc.CloseKey;
RegOdbc.OpenKey('Software/ODBC/ODBC.INI/' + OdbcName, True);
GetSystemDirectory(WinSystemPath, 40);
RegOdbc.WriteString('DBQ', CurrPath + SysDatabaseName);
RegOdbc.WriteString('Driver', WinSystemPath + '/odbcjt32.dll');
RegOdbc.WriteString('FIL', 'MS Access;');
RegOdbc.WriteString('UID', '');
RegOdbc.WriteInteger('DriverId', $19);
RegOdbc.WriteInteger('SafeTransactions', $0);
finally
RegOdbc.Free;
end;
//************** 打开ODBC数据源 *****************************//
dbParam:= 'ODBC DSN=' + OdbcName + #13#10 +
'USER NAME=' + #13#10 +
'PASSWORD=tfnet' + #13#10;
try
dbIP.DriverName:= 'Microsoft Access Driver (*.mdb)';
dbIP.Params.Text:= dbParam;
dbIP.Open;
except
on e: Exception do
begin
Application.MessageBox(PChar('数据库连接错误, 请检查数据库配置。'#13#10), '错误提示', MB_ICONERROR);
//Application.Terminate;
end;
end;
end;