Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve data from sqlserver in vb.net using entity framework and native sql

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?

like image 232
Khushi Avatar asked Jan 18 '26 15:01

Khushi


1 Answers

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.

like image 121
OneFineDay Avatar answered Jan 20 '26 08:01

OneFineDay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!