Lab 3
Lab 3
Lab 3
LABORATORY WORK №3
Information system development
«Connection Database with Windows Form Application C#»
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.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
2.2 Design part for creation design. Here you can add buttons
and other elements of design.
56
3.2 Add button. The same way as 3.1 add button to form
3.4 Open event list and make double click on “Click” event
60
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
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
3.10
private void button1_Click(object sender, EventArgs e)
{
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
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.
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.
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
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.
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