0% found this document useful (0 votes)
55 views

Lab 8

This document provides instructions for a database systems lab assignment involving ADO.NET. Students are asked to create two Windows forms - one with a button to open the other, and that other form displays retrieved data in a DataGridView and has buttons to load and exit the form. Code examples are provided for retrieving data from a SQL database into a DataGridView using ADO.NET objects like SqlConnection and SqlCommand. Students add columns, load data on form load, and add rows from a data reader to complete the task.

Uploaded by

ibrahim zayed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Lab 8

This document provides instructions for a database systems lab assignment involving ADO.NET. Students are asked to create two Windows forms - one with a button to open the other, and that other form displays retrieved data in a DataGridView and has buttons to load and exit the form. Code examples are provided for retrieving data from a SQL database into a DataGridView using ADO.NET objects like SqlConnection and SqlCommand. Students add columns, load data on form load, and add rows from a data reader to complete the task.

Uploaded by

ibrahim zayed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TAFILA TECHNICAL UNIVERSITY

Faculty of Engineering
Department of Communication and Computer Engineering

Database Systems Lab


Lab 8
ADO.NET

Lab Objectives:

- Using the SQL statement in ADO.NET


- Using the DataGridView Tool to display the rows returned from the SQL statement.
- Working with Multiple Windows Forms.

Introduction:

In this lab you will establish a connection to the MSSQL Server as you have done in the previous lab to
perform a SQL statement and display the retrieved row in a DataGridView tool.
Therefore, interacting with a database usually involves these three steps:

1) Establishing a connection to the database server


2) Manipulating data, by executing SQL statement to retrieve data from the server.
3) Closing the connection with the server.

The following objects should be created:

1) A “SqlConnection con” object. Your code should include a statement similar to SqlConnection con =
new SqlConnection();
2) A “SqlCommand cmd” object. Your code should include a statement similar to
SqlCommand cmd = new SqlCommand(strsql, con);
Where strsql is the required SQL statement and con the connection object
3) And finally a “SqlDataReader myreader” object. Your code should include a statement similar to
myreader = cmd.ExecuteReader();

The SqlConnection object is used to create a connection to the database, the SqlCommand object is used to
identify the SQL statement and the SqlDataReader object is used to execute SQL statements and return the
results.

Creating a connection:

*This step is exactly the same as you did in the previous task.
*You can establish a connection by creating a “SqlConnectionStringBuilder” Object (s) and assigning the
“ConnectionString” property of “s” to the ConnectionString property for the connection object “con”.

SqlConnectionStringBuilder s = new SqlConnectionStringBuilder(


"Data Source= localhost;Initial Catalog=test;Integrated Security=True;Pooling=False");
con.ConnectionString = s.ConnectionString;
Creating a statement:

A statement is created by passing the required SQL statement as the first parameter for SqlCommand
constructur and con as the second parameter.

string strsql = "select * from table1";

SqlCommand cmd = new SqlCommand(strsql, con);

Retrieving the result set:

The myreader object is filled with data by calling the ExecuteReader() method in the SqlCommand object

myreader = cmd.ExecuteReader();

You access the data in a myreader object through a cursor. This cursor is a pointer that points to one row of
data in the myreader. Initially, the cursor is positioned before the first row. The method myreader.Read()
moves the cursor to the next row. To read the value of a certain column in the current row the
myreader["columnName "] indexer is used and the ToString() function is used to convert a specific data type
to string .

Required Task

You need to add a new button to your previos Windows Form and name it “Show All Employees” as shown in
the next page, this button will make a call to the second Windows Form which you will add to your project.
To add a new form to you application you need to select your WindowsFormApplication in the solution
explorer pane, then right click and select "Add" then select Windows Form. The Add New Item Window will
be opened for you, select windows form template and you can give it a name and then press the ADD button,
the new added form will be displayed in your Application in the solutio explorer pane. You need to add a
button “Exit”and a DataGridView tool to your new windows form as shown in the second Form in the next
page, so from the tool box window drag and drop the DataGridView into your form.

You can give the DataGridView a meaningfull name in the properties window. Let us say that you named it
employee_view. There are many useful methods to manipulate the DataGridView as follow:

dataGridView1.Rows.Clear();//used to clear all the rows in the DataGridView.

dataGridView1.Columns .Add(string columnName, string headerText); //used to add a column to the


DataGridView. The first parameter represents the column name and the second one represents the column
heading which will be displayed in the DataGridView, and it is usually put in the Form_load() method to be
executed once when the form is loaded for the first time.

Example: the following code is used to add a column named fname with a First Name as a heading for it:

dataGridView1.Columns.Add("fname", "First Name");


dataGridView1.Rows.Add(params object[] values); //used to add a row to the DataGridView. The values
parameter represent the row to be added to the DataGridView which returned from the ExecuteReader()
methode as mintioned in the Retrieving the result set section above.

Example: the following code is used to add a new row to the DataGridView:

dataGridView1.Rows.Add(myreader["fname"].ToString(),myreader["lname"].ToString(),
myreader["ssn"].ToString(),myreader["bdate"].ToString(),myreader["salary"].ToString());

The “Show All Employees” button will call the second form, so you need to create a new object from the
send form and call the Show() method by that object as follow:

Form2 f2 = new Form2();


f2.Show();

The “Exit” button in the second form will hide the form itself, to do that you need to call the Hide() method
by using the this object which represent the active form you working on as follow:

this.Hide();
Sample Code:The code below shows the steps you need to perform in the second Form.
//system gennerated namespace
using System.Data.SqlClient; //This namespace must be added

namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
//write your code to add columns to the DataGridView
}
private void Display_ALL_Employee_Click(object sender, EventArgs e)
{ //clear the DataGridView Rows
//create a connection to the Database as you did in the last lab
//create your SQL statement here
//define a SQL command object
//call the ExecuteReader()to execute your SQL statement
//crete a loop to retrieve the rows from the table and add them to the
DataGridView
//close the connection to the Database as you did in the last lab
}
}
}
Exercise:

- Run the MS Visual Studio and open your previous project.


- Design the Windows forms as displayed above.
- Add the necessary code to the “Show all employees “ button you just added to the first form ,and the
necessary code to the “Display all employees” and the “exit” buttons you added to the second Form
- Run your application and use it to insert the following rows. After inserting each row you should
press the “Show all employees” button to open the second Form then press the “Display all
employees” button to check if the row inserted successfully or not. Your lab supervisor should see
you insert at least one row in front of him/her :
o John, Smith, 2010974040, 03-Jan-1990, 775
o Frank, Wong, 2011975050, 07-Feb-1980, 668
o John, Borg, 200973078, 09-Dec-1999, 888

Submission:

- You must submit a report containing the following:


o The code you have written for the Form2_Load method.
o The name and the text values you specified for the” Show all employees”, “Display all
employees”, and the “Exit” buttons in the properties window.
o The name you specified for the “DataGridView” in the properties window.
o The code you have written for the” Show all employees”, “Display all employees”, and the
“Exit” buttons.
o Conclusions: What did you learn in this lab task

You might also like