Nothing Special   »   [go: up one dir, main page]

Lab 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

51

LABORATORY WORK №3
Information system development
«Connection Database with Windows Form Application C#»

Aim: learn to connect a database to a Windows desktop


application

Tasks:
1. Development of an application for Windows Form
Application C # "
2. Connecting the database to the application

Guidelines

Part I
1.1 Start VS2010. You can find it on the list of programs.
52

1.2 Main page of VS2010:

1.3 In the upper left corner you can see button “File” press it.
53

1.4 In the dialog, select the button “New” after press “Project”
as you can see there is a hot key combination “Ctrl+Shift+N” it also
can be used.

1.5 In window “New Project” make sure that “Visual C#” has
chosen after that make one mouse click on “Windows Forms
Application”. Last step is write a name of your project and press “OK”
54

1.6 The project was created it is the end of part I


55

Part II Controls and basic functions.


2.1 Solution explore. In this part you can find all references.
Design part and .cs files.

2.2 Design part for creation design. Here you can add buttons
and other elements of design.
56

2.3 Toolbox this menu contains all elements of design


57
58

Part III DataGridView and button_click – action


3.1
In toolbox find element with name “DataGridView” and add it
to Form. By mouse drag.

3.2 Add button. The same way as 3.1 add button to form

3.3 Create button_click event. Make right mouse click on button


and in menu chose “Properties”
59

3.4 Open event list and make double click on “Click” event
60

3.5 VS will automatically redirect you to .cs page and also


automatically create button1_click event

3.6 Add this cod for connection with database. And add “using
System.Data.OleDb”.
Make sure that location of DB is correct in “OleDbConnection”
and extension of DB is .mdb
61

OleDbConnection conn = new


OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\\forc.mdb");
OleDbCommand com = new OleDbCommand();
OleDbDataAdapter adap = new OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
com.Connection = conn;
com.CommandText = "Select * from Tab1";
conn.Open();
adap.SelectCommand = com;
adap.Fill(ds);
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
conn.Close();

3.7 Press “Start debugging” button


62

3.8 If you done all steps correct you will see your application.
Press button and get result.

3.9
private void button1_Click(object sender, EventArgs e)
{
63

OleDbConnection conn = new


OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\\forc.mdb");
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Tab1 ([Name],[Fname])
values (?,?)";
cmd.Parameters.AddWithValue("@Name", "qwer");
cmd.Parameters.AddWithValue("@Fname", "qaz");
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show("An Item has
been successfully added", "Caption",
MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
conn.Close();
}

3.10
private void button1_Click(object sender, EventArgs e)
{

OleDbConnection conn = new


OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\\forc.mdb");
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;

for (int i = 0; i < dataGridView1.Rows.Count-1; i++)

// cmd.CommandText = "update Tab1


set([Name],[Fname],[Age],[Phone]) where values (?,?,?,?)";
cmd.Parameters.AddWithValue("@Name",
dataGridView1.Rows[i].Cells["Name"].Value.ToString());
cmd.Parameters.AddWithValue("@Fname",
dataGridView1.Rows[i].Cells["Fname"].Value.ToString());
cmd.Parameters.AddWithValue("@Age",
dataGridView1.Rows[i].Cells["Age"].Value.ToString());
64

cmd.Parameters.AddWithValue("@Phone",
dataGridView1.Rows[i].Cells["Phone"].Value.ToString());
cmd.Parameters.AddWithValue("@var",
dataGridView1.Rows[i].Cells[0].Value);
cmd.CommandText = "UPDATE Tab1 SET
Name=@Name, Fname=@Fname, Age=@Age,
Phone=@Phone WHERE Код=@var";

cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();

cmd.Parameters.Clear();
conn.Close();
}

System.Windows.Forms.MessageBox.Show("An Item
has been successfully added", "Caption",
MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
conn.Close();
}

3.11
OleDbConnection conn = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\\forc.mdb");
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "DELETE * FROM Tab1 WHERE
Name = ?";
cmd.Parameters.AddWithValue("@Name", "A");

cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
65

System.Windows.Forms.MessageBox.Show("An Item
has been successfully added", "Caption",
MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
conn.Close();

Control questions
66

PART 2 Web Forms in Visual Studio 2010


Aim of the Laboratory work: The purpose of the laboratory
work is to familiarize students with the ASP.NET Web Application.

CREATING A NEW ASP.NET PROJECT


Start Visual Studio 2010 and select New Project from the File
menu. You will see the New Project dialog box, which, as the name
suggests, you use to create new Visual Studio projects.
You will see a list of the available project types in the left-hand
panel of the dialog box. Navigate to Installed Templates ➤ Visual C#
➤ Web and you will see the set of ASP.NET projects available, as
shown in Figure 1-1.

Figure 1-1. The New Project dialog box

Select the ASP.NET Empty Web Application item from the


central panel of the dialog box—some of the names of the different
project types are similar so make sure that you get the right one. Make
sure that .Net Framework 4 is selected in the drop-down menu at the
top of the screen and set the Name field to “test”. Click the OK button
to create the new project.
■ Tip Visual Studio will set the Solution Name field to “test” to
match the project name. A Visual Studio solution is a container for
67

one or more projects, but for all of the examples in this book our
solutions will contain just one project, which is typical for
ASP.NET Framework development.
The ASP.NET Empty Web Application is the simplest of the
project templates and creates a project that only contains a Web.config
file, which contains the configuration information for your ASP.NET
application. Visual Studio shows you files in the Solution Explorer
window, which you can see in Figure 1-2. The Solution Explorer is the
principal tool for navigating around your project.

Figure 1-2. The Visual Studio Solution Explorer window

ADDING A NEW WEB FORM


To add a new Web Form to the project, right-click the “test”
project entry in the Solution Explorer window and select Add ➤ New
Item as shown in Figure 1-3.
68

Figure 1-3. Adding New Item

Then select Web Form from the central panel of the dialog box.
When prompted, enter Default as the name for the new item, as shown
in
Figure 1-4. And then press Add button.
69

Figure 1-4. Setting the name for the new Web Form

You will see that Visual Studio has added a Default.aspx file to
the project in the Solution Explorer and opened the file for editing.
You can see the initial contents of the file in Listing 1-1.

Listing 1-1. The initial contents of the Default.aspx file


70

A Web Form file is, at its heart, an enhanced HTML file. The
element that has the tags gives away the fact this isn’t a regular HTML
file. In Listing 1-2, you can see that we have added some standard
HTML elements to the Default.aspx file.
Listing 1-2. Adding standard HTML elements to the Default.aspx
file

TESTING THE EXAMPLE APPLICATION


Click the small arrow to the right or press F5 in order to start
debugging, as shown in Figure 1-5.

Figure 1-5. Running the Project.

Visual Studio will compile your project and open a new browser
window to display the Web Form, as shown in Figure 1-6. There isn’t
71

much content in the Web Form at the moment, but at least we know
that everything is working the way that it should be.

Figure 1-6. Displaying the Web Form in the browser

Here is the URL that Internet Explorer used for our example:
http://localhost:8507/Default.aspx You will see a similar URL when
you start the application, but it won’t be identical. You will see the
http:// part (specifying that the HTTP protocol is to be used) and the
localhost part, which is a special name that refers to the workstation.
The port part of this URL, 8507 in our case, is assigned randomly and
you will see a different number. The last part of the URL,
Default.aspx, specifies that we want the contents of our Default.aspx
file, that is, what you can see in the browser window.

Control questions

You might also like