Lab 8
Lab 8
Faculty of Engineering
Department of Communication and Computer Engineering
Lab Objectives:
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) 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”.
A statement is created by passing the required SQL statement as the first parameter for SqlCommand
constructur and con as the second parameter.
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:
Example: the following code is used to add a column named fname with a First Name as a heading for it:
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:
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:
Submission: