3

I want to retrieve data in Winforms using vb.net, entity framework and native sql. I have used the code below which allows me to add data to the sql:

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim db As New SampleDBEntities
    Dim bs As New BindingSource
    bs.DataSource = db.Cars.Local
    DataGridView1.DataSource = bs
End Sub

But I don't know how to fire a query to retrieve data from database. Suppose I want to get all the records from Cars table in my Database named SampleDB. So I need "SELECT * FROM Cars", but how to use this query?

2
  • Either you work with SQL directly (through SQL or stored procedures) and communicate with the server using SqlConnection, SqlCommand and perhaps DataReader or DataAdapter or you correctly use the Entity Framework that you have configured and talk to that. Have you set up EF in your project already?
    – Recipe
    Jul 27, 2013 at 17:48
  • yes I have already set up EF in my project
    – Khushi
    Jul 27, 2013 at 17:51

2 Answers 2

4

To get all the cars it would be:

Using db As New SampleDBEntities
  Dim cars = db.Cars.ToList
End Using

To get all cars by type, if you have a 'type' field in that entity.

Using db As New SampleDBEntities
  Dim mazdaCars = db.Cars.Where(Function(c) c.Type = "Mazda").ToList
End Using

ENtity Framework was built for LINQ and Lambda. Be sure to close/dispose your entity container object.

2
  • 1
    I got a solution context.Cars.SqlQuery("SELECT * FROM Cars").ToList(). Thanks anyways for trying to answer my question.
    – Khushi
    Jul 27, 2013 at 18:01
  • Hey @OneFineDay, I am starting a Windows forms project using EF and VB.NET, I don't know how to access the data from my vb file. Do you have any idea or tutorial to give me please ?
    – ben
    Nov 30, 2016 at 9:47
3

Either you work with SQL directly (through SQL or stored procedures) and communicate with the server using SqlConnection, SqlCommand and perhaps DataReader or DataAdapter or you correctly use the Entity Framework that you have configured.

I'm not really into VB.Net, so it'll be pseudocode, but you need to be addressing your EF context.

Something like

using (myEntityModel context = new myEntityModel()) 
{
    MyResult = context.Cars.where(c => c.model == "myModel").ToList();
}
2
  • I got a solution context.Cars.SqlQuery("SELECT * FROM Cars").ToList(). Thanks anyways for trying to answer my question.
    – Khushi
    Jul 27, 2013 at 18:01
  • 1
    @user2524507 By entering an SqlQuery into EF, you're completely missing the point of the EF itself. It was designed to be a bridge between your dotNet code and a SQLServer (or the likes). If you want to access SQL directly by injecting SQL statements, you should look at the SqlClient assembly in System.Data and work with classes such as SqlConnection and SqlCommand.
    – Recipe
    Jul 27, 2013 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.