Home

Introduction to the Records of a Database

 

Introduction

A table is an object that holds the information of a database. This means that the database must first exist. Here is an example of creating such a database:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Exercise
{
    public partial class Exercise : Form
    {
	public Exercise()
	{
	    InitializeComponent();
	}

	private void btnDatabase_Click(object sender, EventArgs e)
	{
	    using (SqlConnection connection =
		new SqlConnection("Data Source=(local);" +
				  "Integrated Security=yes;"))
	    {
		SqlCommand command =
		    new SqlCommand("CREATE DATABASE VideoCollection1;",
				   connection);
		connection.Open();
		command.ExecuteNonQuery();

	MessageBox.Show("A database named VideoCollection1 has been created.");
	    }
	}
    }
} 

Here is a sample table:

private void btnDatabase_Click(object sender, EventArgs e)
{
    using (SqlConnection connection =
	new SqlConnection("Data Source=(local);" +
			  "Database='VideoCollection1';" +
			  "Integrated Security=yes;"))
    {
	SqlCommand command =
	    new SqlCommand("CREATE TABLE Videos (" +
			   "[Video Title] varchar(120), " +
			   "Director varchar(100), " +
			   "[ Year] smallint, " +
			   "Length varchar(30), " +
			   "Rating varchar(6));",
			   connection);
	connection.Open();
	command.ExecuteNonQuery();

	MessageBox.Show("A table named \"Videos\" has been created.");
    }
} 

Because a table is the central part of a database, the information it holds must be meticulously organized. To better manage its information, data of a table is arranged in a series of fields called cells. Once a table contains information, you can review it using either the Microsoft SQL Server Management Studio or a Windows application.

Practical LearningPractical Learning: Introducing Database Records

  1. Start Microsoft Visual C# and create a new Windows Application named CollegeParkAutoRepair3
  2. In the Server Explorer, right-click Data Connections and click Add New SQL Server Database
  3. In the Server Name combo box, select the server or type (local)
  4. Set the name of the database to CPAR2 and click OK
  5. In the Server Explorer, expand server.CPAR2.dbo
  6. Under it, right-click Tables and click Add New Table
  7. Complete the table with following columns:
     
    Column Name Data Type
    ReceiptNumber int
    OrderDate datetime
    OrderTime datetime
    CustomerName varchar(80)
    CarMake varchar(50)
    CarModel varchar(50)
    CarYear smallint
    ProblemDescription text
    TotalParts money
    TotalLabor money
    TaxRate decimal(6,2)
    TaxAmount money
    TotalOrder money
    Recommendations text
  8. To save the table, on the Standard toolbar, click the Save button
  9. Type RepairOrders as the name of the table and click OK
  10. Close the RepairOrders window
  11. In the Solution Explorer, right-click Form1.cs and click Rename
  12. Type OrderProcessing.cs and press Enter twice
  13. Design the form as follows:
  14. Design the form as follows:
     
    College Park Auto Repair 
    Control Name Text Other Properties
    Group   Order Identification  
    Label   Order Date/Time:  
    DateTimePicker dtpOrderDate    
    DateTimePicker dtpOrderTime   Format: Time
    ShowUpDown: True
    Label   Customer Name:  
    TextBox txtCustomerName    
    Label   Make / Model:  
    TextBox txtMake    
    TextBox txtModel    
    Label   Year:  
    TextBox txtCarYear   TextAlign: Right
    Label   Problem Description:  
    TextBox txtProblemDescription   Multiline: True
    ScrollBars: Vertical
    GroupBox   Order Summary  
    Label   Total Parts:  
    TextBox txtTotalParts 0.00 TextAlign: Right
    Label   Total Labor:  
    TextBox txtTotalLabor 0.00 TextAlign: Right
    Label   Tax Rate:  
    TextBox txtTaxRate 7.75 TextAlign: Right
    Label   %  
    Label   Tax Amount:  
    TextBox txtTaxAmount 0.00 TextAlign: Right
    Label   Total Order:  
    TextBox txtTotalOrder 0.00 TextAlign: Right
    Label   Recommendations  
    TextBox txtRecommendations   Multiline: True
    ScrollBars: Vertical
    Button btnSaveRepairOrder Save Repair Order  
    Button btnNewRepairOrder New Repair Order/Reset  
    Button btnClose Close  
  15. Double-click the New Repair Order button and implement its event as follows:
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace CollegeParkAutoRepair3
    {
        public partial class OrderProcessing : Form
        {
    	public OrderProcessing()
    	{
    	    InitializeComponent();
    	}
    
    	private void btnNewRepairOrder_Click(object sender, EventArgs e)
    	{
    	    dtpOrderDate.Value = DateTime.Today.Date;
    	    dtpOrderTime.Value = DateTime.Now;
    	    txtCustomerName.Text = ""; txtCarMake.Text = "";
    	    txtCarModel.Text = ""; txtCarYear.Text = "";
    	    txtProblemDescription.Text = "";
    
    	    txtTotalParts.Text = "0.00"; txtTotalLabor.Text = "0.00";
    	    txtTaxRate.Text = "7.75"; txtTaxAmount.Text = "0.00";
    	    txtTotalOrder.Text = "0.00"; txtRecommendations.Text = "0.00";
    	}
        }
    }
  16. Return to the form

Table Data Navigation

Data Navigation consists of displaying and viewing data. Because information of a database is stored in tables, your primary means of viewing data consists of opening a table in a view that displays its information.

When a table displays its records, you navigate through its fields using the mouse or the keyboard. With the mouse, to get to any cell, you can just click it. To navigate through records using the keyboard, you can press:

  • The right arrow key to move to the right cell; if the caret is already in the most right cell, it would be moved to the first cell of the next record, up to the last empty cell of the first empty record
  • The left arrow key to move to the previous cell; if the caret is in, or reaches, the most left cell of the first record, nothing would happen when you press the the left arrow key
  • The down arrow key to move to the cell under the current one; if the caret is already in the last cell of the current column, nothing would happen
  • The up arrow key to move to the cell just above the current one; if the caret is already in the first cell of the current column, nothing would happen
  • The Page Down to move to the next group of cell that would correspond to the next page; if the number of records is less than a complete page, the caret would move to the last cell of the current column
  • The Page Up to move to the next group of cell that would correspond to the next page; if the number of records is less than a complete page, the caret would move to the first cell of the current column
 

Published on Thursday 03 January 2008

 

Home Copyright © 2007 FunctionX, Inc. Next