Pheonix
Pheonix

Reputation: 6052

how to query/get data from Datasource in VB.NET

i am building a database application in vb.net and i started by adding a data source from the DATA in the toolbar. my connection is good and it shows all my tables in the data source panel.

i also see new classes related to my database, like

sakilaDataSet
sakilaDataSet.customerDataTable

... and so on.

how do i query and use these ? i googled a lot and i am not able to get this.

Dim cust As sakilaDataSet.customerDataTable = New sakilaDataSet.customerDataTable
        Dim row() As System.Data.DataRow = cust.Select("customer_id=5")
        MsgBox(row.Count)

My last try was with the above code, but the row.count always turns out to be zero.

Upvotes: 0

Views: 10793

Answers (3)

PsychoData
PsychoData

Reputation: 1304

This website http://visualbasic.about.com/od/usingvbnet/a/begdbapp7.htm had a good tutorial where it talked about what happens when you use the data sources window and how to use it in your code after that.

I know it's an old question, but it was very high in Google results, and it was more of a "This is how you should have done it" answer instead of actually answering with what it seems like you asked

Upvotes: -1

Pheonix
Pheonix

Reputation: 6052

What i wanted to achieve was not to use connection strings again. After adding the data source in VB.net, it makes Data Classes and Adapters, which i can use directly to access the database, as follows :

Dim staff As sakilaDataSet.customerDataTable = New sakilaDataSetTableAdapters.customerTableAdapter().GetData
Dim rows() As sakilaDataSet.customerRow = staff.Select("email='" + email.Text + "'")

Upvotes: 1

Gabe Thorns
Gabe Thorns

Reputation: 1426

You need to open a connextion to the DB. Here are some options:

You could use EntityFramework, which provides a nice way to access data and control it by mapping to entities (classes). For this, in VisualStudio create a ClassLibrary project, add an item ADO.NET Entity Data Model. This will open a wizard that will help you connect to the DB, map the objects in the DB to entities and access the entities by a reference to an entity context. The basics are easy.

Other option is to use an OLEDB provider, which is a classic way to access DB. An example to open an Access DB of employees:

Dim connString As String = "provider= microsoft.jet.oledb.4.0; " & _
     "data source=Employee.mdb;"

  Dim conn As New OleDbConnection(connString)

  Try
     conn.Open()
  Finally
     conn.Close()
     Console.WriteLine("Connection Closed")
  End Try

Visit http://www.connectionstrings.com/ to get a list of common connection string for many DB. Other useful links:

EntityFramework:

http://www.codeguru.com/csharp/csharp/net30/article.php/c15489

http://www.asp.net/entity-framework/tutorials

OLEDB:

http://oreilly.com/catalog/progvbdotnet/chapter/ch08.html

http://www.homeandlearn.co.uk/net/nets12p2ed.html

http://www.sourcecodester.com/tutorials/net/database-programming-made-easy.html

Hope this helps.

Upvotes: 1

Related Questions