That's rather a broad question. There are several ways to access data in .NET. I'll describe just one and point you to some resources to get you started learning the ADO ropes. I'll start with a very brief overview of ADO.NET.
There are two central components of ADO.NET to access data: the DataSet and the .NET data provider, which is a set of components including the Connection, Command, DataReader and DataAdapter objects.
The DataSet is designed to provide access to data independent of the source. As a result, you can use it with many disparate dataset transparently to your application, including XML. The DataSet contains a collection of one or more DataTables which are the rows and columns of data along with primary key foreign key, constraint and relational information about the data.
The .NET data provider has components which are designed for data manipulation and fast, forward-only, read-only access to data. These include:
- The Connection object
- - provides connectivity to a data source.
- The Command object
- - enables access to database commands to return data, modify data, run stored procedures, etc.
- The DataReader
- - provides a high-performance stream of data from the data source.
- The DataAdapter
- - the bridge between the DataSet object and the data source.
If you don't need the features of a DataSet, you can increase the efficiency of your application by using a Datareader instead. You can use the ADO.NET DataReader to retrieve a read-only, forward-only stream of data from a database. Using the DataReader can increase application performance and reduce system overhead because only one row at a time is ever in memory.
Let's use a simple scenario: We'll connect to an access database and fill a dataset with data from one table. First, in your project, create a new data connection. You can right click on Data Connections in the server explorer and select Add New... Notice there is an option for creating a SQL database there as well. In the properties dialog, select the provider tab and then select one of the JET providers (if you're connecting to another database, select the appropriate provider). On the connection tab browse to your Access database (or fill in the required info for a SQL database if that's what you chose for a provider). Set up any login info and your ready to go. First, though, check the advanced tab and set your access level appropriately. Click OK. Notice you now have a tree control listing of database objects. This data here is 'live', that is you can open a table and change the data. Be ye careful!
Now drag and drop the connection from the server explorer to a VB form. You'll se it appear along the bottom of the window. Give it a name you like, I'll simply call mine DA, but you should choose a better name. Now that we have a connection, let's get some data.
' Create a dataset to hold the data Dim DS As New DataSet() ' Fill the dataset with all data from certifications table DA.Fill(DS, "Certifications") ' Create a datarow object to hold data from one row Dim DR As DataRow = DS.Tables(0).Rows(0) ' Display data for the first column in the row MsgBox(CStr(DR.Item(0)))
Place this code inside a method, perhaps a button click event. It will simply create a DataSet, fill with data for the certifications table, create a DataRow and display the data in the first column of the DataRow. This is just a very simple example. Most of the methods of the ADO objects are overloaded and provide many options. The DataSet can be used to CRUD work, so you can add data, update it, delete it, etc.
Now to learn more about accessing data in .NET see this topic in MSDN, or search MSDN for "accessing data with ADO.NET".
Dig Deeper on Topics Archive
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.