I have bought a SSD with M.2 socket (B & M) key, but my computer just have a B key socket. My question is if I can connect the SSD in this B socket, and would it work correctly?
It should... a B&M module should work with either a B or M socket, this is by design.
The B interface provides x2 PCIe, SATA, USB 2.0 and 3.0, audio, UIM, HSIC, SSIC, I2C, and SMBus support. The M interface provides x4 PCIe, SATA, and SMBus.
If your module is B&M SATA (most likely), connecting it to a B or M socket should work fine since both types support SATA.
Of course, being a bootable device is another story, this depends on your hardware and UEFI.