function TDataModule1.GetConnectionString(aType, aUser, aPwd, aDataBase, aServer: string): string; begin Result := ''; if aType = 'ACCESS' then begin Result := 'ProviderMicrosoftJetOLEDB40Persist Security InfoFalse'; Result := Result + 'Data Source' + aDataBase; end; if aType = 'SQL' then begin Result := 'ProviderSQLOLEDB1Persist Security InfoFalse'; Result := Result + 'User ID' + aUser + ''; Result := Result + 'Password' + aPwd + ''; Result := Result + 'Initial Catalog' + aDataBase + ''; Result := Result + 'Data Source' + aServer; end; if aType = 'ODBC' then begin Result := 'ProviderMSDASQL1Persist Security InfoFalse'; Result := Result + 'Data Source' + aServer + ''; Result := Result + 'Initial Catalog' + aDataBase; end; end;
function TDataModule1.TestConnect: Boolean; begin Result := True; //数据初始化连接 ADOConnection.Connected := False; ADOConnection.ConnectionString := GetConnectionString('SQL', FUser, FPwd, FDataBase, FServer); try ADOConnection.Connected := True; except Result := False; Application.MessageBox('基本数据库连接错', '错误', MB_OK + MB_ICONERROR); end;
if Result then Application.MessageBox('数据库连接成功', '信息', MB_OK + MB_ICONINFORMATION); end;
function TDataModule1.Test: Boolean; begin Result := True; //数据初始化连接 ADOConnection.Connected := False; ADOConnection.ConnectionString := GetConnectionString('SQL', FUser, FPwd, FDataBase, FServer); try ADOConnection.Connected := True; except Result := False; end; end;