Hello,
InfoPath has default functionality that allows you to retrieve data from the
DB without having to write code. If you write code to retrieve data then you
will have to give full trust to the form.
How to retrieve data from the DB directly via InfoPath
1. Click on Tools -> Data connections
2. Click on Add and select Receive in the radio button options and click on
Next
3. Select Database and click on Next
4. Click on Select Database and create a data connection to the database.
This is place where you edit the SQL query specifically if you so wish by
clicking on Edit SQL.
5. Click on Next and then Click on Finish
From now on whenever the form is opened it will retrieve the latest
information from the database.