Home » C# Basics » 10 - Data Access
10

Creating a Database - Part 2

How to access data stored in SQL Server 2005 using the .Net framework and C# programming

Now that we have our sample data stored in the table, we can create an application to use this data.

The .NET framework has a set of classes that is used for working with the data. This set of classes is known as ADO.NET. Using the properties and methods of the ADO.NET classes, we can write code in C# to access the data stored in a MS Access, SQL Server, Oracle or XML database. However, writing code for data access is a complex task for a beginner. Fortunately, we have another option to access data without writing any code at all! This is known as Data Binding. We will use data binding in our application to access data:

1. From the Data menu of the Visual C# IDE, select the Add New Data Source... option. This will start the Data Source Configuration Wizard.

2. The first step in the wizard is to select the source of the data. Since our data is stored in a SQL Server 2005 database, select the Database option.

Click on the Next button to proceed.

3. Next we should select the Data connection. Since we only have one database, its name is displayed in the drop down list. If we have multiple databases, we can select the required one from the drop-down list. We can also create a new database connection from here if we want to.

4. The next step will ask us if we want to store the connection string in the application's configuration file. The connection string is actually a text string that contains information about our data connection. While working in visual drag and drop mode like we are working now, there is no need to know about the connection string. If we save the connection string into the application's configuration file, this makes the application easy to maintain. Hence, it is recommended that we store the connection string into the application's configuration file

5. In the final step, we will specify the table from which we want to access the data. Expand the Tables node by clicking on the + symbol and select the name of the table:

Click on the Finish button to close the wizard. We have successfully added a data source to our application.

Now is the time to implement this data source in our application.

1. From the Data menu of the IDE, select Show Data Sources option. Alternately, we can click on the Show Data Sources tab, which is located next to the Toolbox tab. The Data Sources window will open, hiding the toolbox window.

2. The Data Sources window will display the name of the table in which we have stored the actual data. Expand the table name by clicking on the + symbol. Now the columns of the table will be displayed.

3. Drag the first column name and drop it on the form. As soon as we drop the column, a label and a textbox control will appear on the form. The text of the label is the name of the column. A new control - BindingNavigator control - is added to the top of the form.

The form and the columns that we drag and drop are converted into text boxes. Each text box will display the data from the corresponding column of the table. The BindingNavigator control provides the options for viewing the records using navigation buttons (first, previous, next and last). It also has buttons for adding new record, deleting a record and saving changes. We are not required to write any code for these functions. Various Binding Components and the IDE will do the job for us.

Drag and drop all the columns (except BookSerialNo column) on the form as shown:

4. Save the project and then run it. The application will start and we can see the first record:

Using the navigation buttons, we can move to the other records. The + button is used to add a new record. The currently displayed record can be deleted using the cross button. We can change an existing record by clicking inside a text box and modifying the data. The save button is used to save any changes.

5. We can use the DataGridView control to display multiple records in a row-column format. Let us add this control to our form. Drag the control from the Data section of the ToolBox and drop it on the form:

As soon as we drop the control on the form, its smart tag menu will open up. From the Choose Data Source drop-down list, select bookDetailsBindingSource option. Also, check the options - Enable Adding (Add a new record in the grid view), Enable Editing (modify a record in the grid view), Enable Deleting (delete a record in the grid view) and Enable Column Reordering (change the order of the columns in the grid view).

Save and run the project:

As shown in the above figure, the grid will display all the records in the table. A blank row at the end is provided for adding a new record. We can modify the data by clicking inside a cell. The buttons provided on the BindingNavigtor control can be used with the DataGridView control as well. We can sort the columns in ascending or descending order by clicking on the column name.

We have achieved the purpose of data access using the Data Binding feature without writing a single line of code!