People on both sides of the aisle who have worked with Mr. Bloomberg’s political operation said that he was always strategic about his alliances on Capitol Hill. He was in constant contact with Republican and Democratic leaders, who tried to keep him close and sought out his help in key races even when his contributions were working against them elsewhere.

“They didn’t ever operate completely in a silo,” said Josh Holmes, Mr. McConnell’s former chief of staff. “They were talking with Republicans; they were talking with Democrats. And they wanted to win. I probably disagree with 90 percent of what they’re doing. But they’re extremely effective.”

For example, even as he made Mr. Toomey’s re-election a priority in 2016, his money — nearly $4.3 million — simultaneously went toward defeating Senator Kelly Ayotte in New Hampshire, a Republican who opposed the background check bill.

Mr. Bloomberg’s giving fits with his political background. He was a longtime Democrat when he switched his registration to run for mayor as a Republican in 2001. He left the Republican Party in 2007 and chose to be unaffiliated with either party until 2018 when he rejoined the Democratic Party.

The Toomey-McGinty race in Pennsylvania is the one that left the most bitter taste for many Democrats because they saw no path to retaking the Senate without winning that seat. Some said they found it frustrating that Mr. Bloomberg did not agree that it would be more helpful to his long term goals on gun control to have a Democrat in that seat and be one step closer to a Democratic-controlled Senate than it would be to see Mr. Toomey re-elected.

In an interview with The New York Times in 2013, Mr. Bloomberg explained his logic, saying that if Democrats could not get senators on board with something as popular with the public as background checks, they should pay a political price. “What I would suggest is that they have all of their members vote for things that the public wants,” he said. “And if they don’t do that, the voters should elect different senators who will listen to them.’’