Wednesday, February 17, 2010

C Sharp Window Forms

This page contains a number of C# codes for the Amateur/Beginners in the Visual C# and .Net PlatForm Environment.

This Tutorial will take anyone from the scratch to a good Windows Form creator although I am still learning at my spare time.

Source Code:

//MyForm1.cs
//This Tutorial will Teach you how to create a Form without Caption Heading
using System;
using System.Drawing;
using System.WinForms;
class MyForm : System.WinForms.Form
{
public MyForm()
{
}
public static void Main()
{
Application.Run(
new MyForm());
}
}
/*
To Compile make a batch File in Dos Mode as compile.bat
csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll
/r:Microsoft.Win32.InterOp.dll /out:MyForm1.exe MyForm1.cs
*/

OutPut:

Source Code:

//MyForm2.cs
//This Tutorial will Teach you how to create a Form with Caption Heading
using System;
using System.Drawing;
using System.WinForms;
class MyForm : System.WinForms.Form
{
public MyForm()
{
//Text to be Displayed in the Caption-Title Bar
this.Text = "Form Tutorial No.2 From JAYANT";
}
public static void Main()
{
Application.Run(
new MyForm());
}
}
/*
To Compile make a batch File in Dos Mode as compile.bat
csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll
/r:Microsoft.Win32.InterOp.dll /out:MyForm2.exe MyForm2.cs
*/

OutPut:

Source Code:

//MyForm3.cs
//This Tutorial will Teach you how to create a Form with Added Functionality describing Size
using System;
using System.Drawing;
using System.WinForms;
class MyForm : System.WinForms.Form
{
public MyForm()
{
//Text to be Displayed in the Caption-Title Bar
this.Text = "Form Tutorial No.3 From JAYANT";
this.StartPosition = FormStartPosition.CenterScreen;
this.AutoScaleBaseSize = new Size(5,13);
this.ClientSize = new Size(400, 200); //Size except the Title Bar-CaptionHeight
this.MinTrackSize = new Size(400, (200 + SystemInformation.CaptionHeight) );
this.MaximizeBox = false;
}
public static void Main()
{
Application.Run(
new MyForm());
}
}
/*
To Compile make a batch File in Dos Mode as compile.bat
csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll
/r:Microsoft.Win32.InterOp.dll /out:MyForm3.exe MyForm3.cs
*/

OutPut:

Source Code:

//MyForm4.cs
//This Tutorial will Teach you how to create a Form with Label on the Form
using System;
using System.Drawing;
using System.WinForms;
class MyForm : System.WinForms.Form
{
Label label1;
public MyForm()
{
label1 =
new Label();
label1.UseMnemonic =
true;
label1.Text = "First &Name:";
label1.Location =
new Point(15,15);
label1.BackColor = Color.Pink;
label1.ForeColor = Color.Maroon;
label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;
label1.Size =
new Size (label1.PreferredWidth, label1.PreferredHeight + 2);
//Text to be Displayed in the Caption-Title Bar
this.Text = "Form Tutorial No.4 From JAYANT";
this.StartPosition = FormStartPosition.CenterScreen;
this.AutoScaleBaseSize = new Size(5,13);
this.ClientSize = new Size(300, 200); //Size except the Title Bar-CaptionHeight
this.MinTrackSize = new Size(300, (200 + SystemInformation.CaptionHeight) );
this.AutoScroll = true;
this.MaximizeBox = false;
this.Controls.Add(label1);
}
public static void Main()
{
Application.Run(
new MyForm());
}
}
/*
To Compile make a batch File in Dos Mode as compile.bat
csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll
/r:Microsoft.Win32.InterOp.dll /out:MyForm4.exe MyForm4.cs
*/

OutPut:

Source Code:

//MyForm5.cs
//This Tutorial will Teach Mouse clicking Events and MessageBox(without Title_Heading) calling
using System;
using System.Drawing;
using System.WinForms;
class MyForm : System.WinForms.Form
{
Label label1;
public MyForm()
{
label1 =
new Label();
label1.UseMnemonic =
true;
label1.Text = "First &Name:";
label1.Location =
new Point(15,15);
label1.BackColor = Color.Pink;
label1.ForeColor = Color.Maroon;
label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;
label1.Size =
new Size (label1.PreferredWidth, label1.PreferredHeight + 2);
//Text to be Displayed in the Caption-Title Bar
this.Text = "Form Tutorial No.5 From JAYANT";
this.StartPosition = FormStartPosition.CenterScreen;
this.AutoScaleBaseSize = new Size(5,13);
this.ClientSize = new Size(300, 200); //Size except the Title Bar-CaptionHeight
this.MinTrackSize = new Size(300, (200 + SystemInformation.CaptionHeight) );
this.AutoScroll = true;
this.MaximizeBox = false;
this.Controls.Add(label1);
this.Click += new EventHandler(clicking);
}
public void clicking(object ob, EventArgs e)
{
MessageBox.Show("You clicked on Form Area");
}
public static void Main()
{
Application.Run(
new MyForm());
}
}
/*
To Compile make a batch File in Dos Mode as compile.bat
csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll
/r:Microsoft.Win32.InterOp.dll /out:MyForm5.exe MyForm5.cs
*/

OutPut:

Source Code:

//MyForm6.cs
//This Tutorial will Teach Mouse clicking Events and MessageBox(with Title_Heading) calling
using System;
using System.Drawing;
using System.WinForms;
class MyForm : System.WinForms.Form
{
Label label1;
public MyForm()
{
label1 =
new Label();
label1.UseMnemonic =
true;
label1.Text = "First &Name:";
label1.Location =
new Point(15,15);
label1.BackColor = Color.Pink;
label1.ForeColor = Color.Maroon;
label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;
label1.Size =
new Size (label1.PreferredWidth, label1.PreferredHeight + 2);
//Text to be Displayed in the Caption-Title Bar
this.Text = "Form Tutorial No.6 From JAYANT";
this.StartPosition = FormStartPosition.CenterScreen;
his.AutoScaleBaseSize = new Size(5,13);
this.ClientSize = new Size(300, 200); //Size except the Title Bar-CaptionHeight
this.MinTrackSize = new Size(300, (200 + SystemInformation.CaptionHeight) );
this.AutoScroll = true;
this.MaximizeBox = false;
this.Controls.Add(label1);
this.Click += new EventHandler(clicking);
}
public void clicking(object ob, EventArgs e)
{
MessageBox.Show("You clicked on Form Area","Title_JAYANT");
}
public static void Main()
{
Application.Run(
new MyForm());
}
}
/*
To Compile make a batch File in Dos Mode as compile.bat
csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll
/r:Microsoft.Win32.InterOp.dll /out:MyForm6.exe MyForm6.cs
*/

OutPut:

Source Code:

//MyForm7.cs
//This Tutorial will Teach Mouse clicking Events and changing the Form colour
using System;
using System.Drawing;
using System.WinForms;
class MyForm : System.WinForms.Form
{
Label label1;
public MyForm()
{
label1 =
new Label();
label1.UseMnemonic =
true;
label1.Text = "First &Name:";
label1.Location =
new Point(15,15);
label1.BackColor = Color.Pink;
label1.ForeColor = Color.Maroon;
label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;
label1.Size =
new Size (label1.PreferredWidth, label1.PreferredHeight + 2);
//Text to be Displayed in the Caption-Title Bar
this.Text = "Form Tutorial No.7 From JAYANT";
this.BackColor = Color.BurlyWood;
this.StartPosition = FormStartPosition.CenterScreen;
this.AutoScaleBaseSize = new Size(5,13);
this.ClientSize = new Size(300, 200); //Size except the Title Bar-CaptionHeight
this.MinTrackSize = new Size(300, (200 + SystemInformation.CaptionHeight) );
this.AutoScroll = true;
this.MaximizeBox = false;
this.Controls.Add(label1);
this.Click += new EventHandler(clicking);
}
public void clicking(object ob, EventArgs e)
{
MessageBox.Show("Click will change the Form Color", "Title_JAYANT");
this.BackColor = Color.Red;
}
public static void Main()
{
Application.Run(
new MyForm());
}
}
/*
To Compile make a batch File in Dos Mode as compile.bat
csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll
/r:Microsoft.Win32.InterOp.dll /out:MyForm7.exe MyForm7.cs
*/

OutPut:

Source Code:

//MyForm8.cs
//This Tutorial will Teach Mouse clicking Events and
using System;
using System.Drawing;
using System.WinForms;
class MyForm : System.WinForms.Form
{
Label label1;
TextBox txtbx1;
Button btn1;
Button exit;
public MyForm()
{
label1 =
new Label();
txtbx1 =
new TextBox();
btn1 =
new Button();
exit =
new Button();
label1.UseMnemonic =
true;
label1.Text = "First &Name:";
label1.Location =
new Point(15,15);
label1.BackColor = Color.Pink;
label1.ForeColor = Color.Maroon;
label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;
label1.Size =
new Size (label1.PreferredWidth, label1.PreferredHeight + 2);
txtbx1.Text = "Enter Your Name";
txtbx1.Location =
new Point(15+label1.PreferredWidth+5, 15);
txtbx1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;
txtbx1.BackColor = Color.LightGray;
txtbx1.ForeColor = Color.Maroon;
txtbx1.Size =
new Size (90, 20);
btn1.Text = "&OK";
btn1.Location =
new Point(15+txtbx1.Location.X+txtbx1.Size.Width,15);
btn1.Size =
new Size (50, 20);
exit.Text = "Exit";
exit.Location =
new Point(150,150);
exit.Size =
new Size (90, 20);
exit.BackColor = Color.Maroon;
exit.ForeColor = Color.White;
//Text to be Displayed in the Caption-Title Bar
this.Text = "Form Tutorial No.8 From JAYANT";
this.StartPosition = FormStartPosition.CenterScreen;
this.AutoScaleBaseSize = new Size(5,13);
this.ClientSize = new Size(300, 200); //Size except the Title Bar-CaptionHeight
this.MinTrackSize = new Size(300, (200 + SystemInformation.CaptionHeight) );
this.AutoScroll = true;
this.MaximizeBox = false;
this.Controls.Add(label1);
this.Controls.Add(txtbx1);
this.Controls.Add(btn1);
this.Controls.Add(exit);
btn1.Click +=
new EventHandler(Btn_Clicked);
exit.Click +=
new EventHandler(Ext_Clicked);
}
public void Btn_Clicked(object ob, EventArgs e)
{
if(txtbx1.Text == "Enter Your Name")
MessageBox.Show("You Have'nt Entered Your Name","Title_JAYANT");
else
MessageBox.Show("Hello!!! " + txtbx1.Text,"Title_JAYANT");
}
public void Ext_Clicked(object ob, EventArgs e)
{
Application.Exit();
MessageBox.Show("Successfully Closed","EXIT");
//not Shown! Do you know Why?
}
public static void Main()
{
Application.Run(
new MyForm());
}
}
/*
To Compile make a batch File in Dos Mode as compile.bat
csc /r:System.dll /r:System.Drawing.dll /r:System.WinForms.dll
/r:Microsoft.Win32.InterOp.dll /out:MyForm8.exe MyForm8.cs
*/

OutPut:


CSharp and Working with Data

This article introduces SQL, shows how to use ADO.NET Datasets, and connect to SQL Server and Microsoft Access databases. It introduces data binding and shows how to build your own SQL Query program that works with Access databases.

Working with Data

Database access is the perfect example of source code reuse. The basic operations are always the same—add, retrieve, update, and delete records. The only thing different is the structure of the data. Yet, each database system has its own native programming library that is not compatible with other databases, making it difficult to port applications to other database systems. So, it is not surprising that numerous database libraries have been developed over the years to encapsulate the implementation details of database operations.

In this article, we talk about how to access a database using ADO.NET—the set of classes that .NET provides for database access.

Databases

Most business applications need to access some kind of a database. That database could be a flat file, a directory of files, or it could be a server program that handles data requests from client programs. The later case is most common because database servers are optimized for managing large amounts of data and many concurrent connections. In the case of Microsoft Access, the tables are contained within a file with the MDB extension and the "server" is the JET database engine that your program communicates with using the classes that ADO.NET provides.

A database consists of a collection of tables. Each table is defined as a set of columns and a collection of rows. Each row has a field of data for each column in its table.

A relational database is a database that defines relationships between the tables. Consider the logical relationship between a Customer table and an Orders table. A customer can have many orders, but each order can belong to only one customer. This type of relationship is called one-to-many.

The Customer record can be thought of as the parent record of each Order record belonging to that customer. If you delete the Customer record while it still has orders in the Orders table, then those orders would be orphaned records. A relational database gives us the ability make that logical relationship a physical one by putting constraints or rules on the table, such as not allowing a Customer record to be deleted if it has any Order records, preserving the integrity of the database.

A database table usually defines a primary key to uniquely identify every record in the table. The primary key is normally a combination of one or more fields in the table. If a table contains child records that are linked back to a parent record, then the value of that parent’s primary key is usually stored in the child record. It is that field that provides the link to the parent record. When a table contains a field that stores a primary key from some other table, that field is called a foreign key.

Consider two tables that store Customers and Orders. The Customer table may be defined to hold customer information and a primary key. Table 15.1 shows what a Customer table might look like.

Table 15.1 - Definition of the Customer Table

Field Name
Data Type
CustomerID
int, auto counter, primary key
Name
string
Address
string
Phone
string

The Customer table is defined to store the Name, Address, and Phone of a Customer, as well as a CustomerID field that will uniquely identify a record in the table. The CustomerID field is the primary key.

The Customer and Orders table have a parent-child relationship, with Customers being the parent table. Each Customer can have one or more Orders, giving the tables a one-to-many relationship. The definition of the Orders table is below.

Table 15.2 - Definition of the Order Table

Field Name
Data Type
OrderID
int, auto counter, primary key
CustomerID
int, foreign key
OrderDate
DateTime
ShipDate
DateTime

The Orders table definition stores the OrderID as its primary key. It also stores the CustomerID as the foreign key to link it back to the customer record of the customer who placed the order.

The Northwind Trader Database

An example of a database is the Northwind Trader database, which you can install with Microsoft Access. The Northwind Trader database is a sample database that contains tables for Customers, Orders, Employees, and other data commonly used by businesses. We will use the Northwind Trader database in the examples that follow.

Figure 15.1

Image:Figure15_1.jpg

Introduction to SQL

Most databases use a language called SQL, Structured Query Language, to process their commands. While SQL (commonly pronounced "sequel") is a large topic, the basics are easily grasped by looking at a few statements. The basic operations for any database are to add, retrieve, update, and delete records. The SQL language supports these operations with the following four statements.

The INSERT Statement

The INSERT statement will add new records to a table. To insert a record, you must specify the name of the table and the values for the fields. The following statement inserts a new record into the Customers table of the Northwind Trader database.

INSERT INTO Customers ( CustomerID, CompanyName )
VALUES ('SUD100', 'Jeff Suddeth')

The INSERT statement takes the name of the table that you want to insert a record into. Then, in parentheses, you must specify the list of fields you are setting values to. After the VALUES keyword, there is another set of parentheses that include the list of values to be assigned to the fields. The statement above creates a new Customers record and sets the CustomerID field to "SUD100" and the CompanyName field to "Jeff Suddeth". All other fields will be null unless the field has a default value defined.

The SELECT Statement

Use the SELECT statement to retrieve a set of records based on the criteria you specify. The SELECT statement may return zero or more records depending on the conditions of your selection. The following statement does not specify any selection criteria. It will return the CustomerID and CompanyName fields from every row in the table.

SELECT CustomerID, CompanyName
FROM Customers

To add search criteria to the SELECT statement, you can use the WHERE clause. The WHERE clause takes a comma separated list of conditions. The following select statement will select the record we added above.

SELECT CustomerID, CompanyName
FROM Customers'''


The ADO.NET Classes

ADO.NET is the portion of the .NET library that deals with databases. Table 15.3 lists some of the common ADO.NET classes.

Table 15.3 – Classes of ADO.NET

Class
Description
DataSet
A set of database tables that can be filled from a database and used as a disconnected database
DataTable
A collection data rows and the columns that define their structure
DataRow
A database Record
DataView
A view of the data. Can be filtered for search criteria and sorted
DataRowView
A single row from a DataView
DataRelation
A parent-child relationship between tables


The classes in Table 15.3 organize the data into table structures. They are declared within the System.Data namespace. The DataSet class is one of the most useful classes in .NET; because, it encapsulates a collection of tables and the relationships between those tables. It is essentially a mini database that you can use in memory. You can also fill the DataSet with tables and data from some other database, giving you a local cache of data.


Roll Your Own Database

In this section, we use the ADO.NET classes from Table 15.3 to define a database of customer information. We will define the structure of the database by adding columns to a table named Customers. Then, we will load some sample data into the table and persist the data to an XML file. When the program runs again, we will load the database from the XML file and print the data that it contains. The complete listing is shown in Example15_1.cs.

Listing 15.1

// Example15_1.cs
using System;
using System.Text;
using System.Data;
using System.IO;

namespace csbook.ch15 {

class CustomerDatabase {

public void MakeFile() {
DataSet ds = new DataSet();
ds.DataSetName = "CustomerDatabase";

DataTable custTable = new DataTable();
custTable.TableName = "Customers";

// this will be the primary key
DataColumn custId = new DataColumn("CustID", typeof(int));

custId.AutoIncrement = true;
custId.AutoIncrementSeed = 101;
custId.AutoIncrementStep = 1;
custTable.Columns.Add(custId);

// make this the primary key
custTable.PrimaryKey = new DataColumn[] { custId };

// add some fields for customer data
custTable.Columns.Add(new DataColumn("Name", typeof(string)));

custTable.Columns.Add(new DataColumn("Phone", typeof(string)));

custTable.Columns.Add(new DataColumn("Email", typeof(string)));

// add the table to the DataSet
ds.Tables.Add(custTable);


// add some records to the table
DataRow row;
row = custTable.NewRow();
row["Name"] = "Evy";
row["Phone"] = "123-555-9876";
row["Email"] = "evy@evymail.com";
custTable.Rows.Add(row);

row = custTable.NewRow();
row["Name"] = "Jon";
row["Phone"] = "123-444-7384";
row["Email"] = "jon@jonsmail.com";
custTable.Rows.Add(row);

row = custTable.NewRow();
row["Name"] = "Eric";
row["Phone"] = "123-666-7398";
row["Email"] = "eric@ericsmail.com";
custTable.Rows.Add(row);

// dump to XML
ds.WriteXml("customers.xml");

Console.WriteLine("The file was created");
}

public void ReadFile() {
DataSet ds = new DataSet();
ds.ReadXml("customers.xml");

foreach (DataRow row in ds.Tables[0].Rows) {
// get the data
string name = (string)row["Name"];
string phone = (string)row["Phone"];
string email = (string)row["Email"];

// build the string
StringBuilder builder = new StringBuilder();
builder.AppendFormat
("Name: {0}\t Phone: {1}\t Email: {2}",
name, phone, email);
// write the string
Console.WriteLine(builder.ToString());
}
}

public void Test() {
if (File.Exists("customers.xml")) {
ReadFile();
}
else {
MakeFile();
}
}

static void Main(string[] args) {
CustomerDatabase db = new CustomerDatabase();
db.Test();
}
}
}

Listing 15.1 defines the Customers table to have the structure shown below.

Table 15.4 – The Customers Table

Field Name
Data Type
CustID
int, auto increment, primary key
Name
string
Phone
string
Email
string

To define the table, we create a DataTable object and set its TableName property to "Customers". The DataTable has a property named Columns, which is a collection of DataColumn objects that define the columns of the table. Another important property of the DataTable is the PrimaryKey property. The PrimaryKey property is an array of DataColumn objects. You can set the primary key by creating an array of one or more DataColumns from the table and assigning a reference to that array in the PrimaryKey property.

The DataColumn class has many properties, many of which you won’t care about for the examples here. However, two important properties are the Name and DataType.

The Name property is a string value that names the field. When you access a column of a DataRow you can use this name as an index instead of an integer; so, you don’t have to remember the order of the columns.

The DataType property stores an instance of the Type class containing the type of data the column will store. The data are stored in the fields as object references so you can use any C# data type. If the DataType of a column is int, then you can set the column’s AutoIncrement property to true. If the AutoIncrement property is true, then the column’s value will be set automatically from a counter variable that is incremented every time a new row is created. This guarantees that the field will hold a unique value for each row in the table and makes the field a good candidate to use as a primary key.

Defining the Table Structure

The following code segment defines the Customer table structure. The CustID field is defined as an auto counter field and used as the primary key.

DataTable custTable = new DataTable();
custTable.TableName = "Customers";
// this will be the primary key DataColumn custId = new DataColumn("CustID", typeof(int)); custId.AutoIncrement = true;
custId.AutoIncrementSeed = 101;
custId.AutoIncrementStep = 1;
Columns.Add(custId);
// make this the primary key
custTable.PrimaryKey = new DataColumn[] { custId };
// add some fields for customer data custTable.Columns.Add(newDataColumn("Name", typeof(string)));
custTable.Columns.Add(new DataColumn("Phone", typeof(string))); custTable.Columns.Add(new DataColumn("Email", typeof(string)));

Reading Rows of Data

The DataTable also has a property named Rows that stores the rows of data. The rows are actually objects of the DataRow class. The DataRow stores a collection of values that you can read or write to using an index. You can either use an integer—if you know the order of the columns—or you can use the name of the field. The following code segment loops through the table’s rows and obtains references to the Name, Phone, and Email fields.

foreach (DataRow row in ds.Tables[0].Rows) {
// get the data
string name = (string)row["Name"];
string phone = (string)row["Phone"];
string email = (string)row["Email"];
}

The DataRow stores the values as object references so the data must be cast to the appropriate type before we can use it. In this case, we used strings.

Adding Rows of Data

We obviously need to add new rows to the table, too. To create a DataRow object, we call the Table’s NewRow method. That method will instantiate a DataRow object and set its auto increment field for the primary key. Then, we can set the other values of the record and add the row into the Table’s Rows collection.

DataRow row  = custTable.NewRow();
row["Name"] = "Evy";
row["Phone"] = "123-555-9876";
row["Email"] = "evy@evymail.com";
custTable.Rows.Add(row);

Storing the Data as XML

The DataSet stores a collection of DataTables in its Tables property. To dump the entire DataSet to an XML file, we need to call the DataSet’s WriteXml method, passing the name of the file as a string. Reading the file back in is just a matter of calling the ReadXml method.

ds.WriteXml("customers.xml");   // dump to XML
ds.ReadXml ("customers.xml"); // restore from XML

Filtering the Data with the DataView

The DataView class can be used to create a new view of the table that can be filtered or sorted without affecting the underlying table. The DataView is similar to a table in that it has a collection of rows but the objects in the DataView’s Rows collection are of type DataRowView. You use the DataRowView the same as you would use a DataRow. You use the name of the field as an index to get the value from the column. Once you have a DataView, you can filter or sort the data without affecting the actual table.

You create a DataView by passing a DataTable to the DataView constructor.

// create a view that can be filtered and sorted DataView view = new DataView(ds.Tables[0]); foreach (DataRowView drv in view) { Console.WriteLine(drv["Name"].ToString()); } // find the record for Rick view.RowFilter = "Name = 'Evy'"; foreach (DataRowView drv in view) { Console.WriteLine(drv["Name"].ToString()); }

In the code segment, we create a DataView for the first DataTable in the DataSet’s Tables collection. Then, we loop through the Rows collection, printing out the Name field as we go. Then, we filter the view so that view only contains records where the Name field is "Evy". The foreach loop iterates through each row in the view of "Evy" records and prints out the Name field.

Table 15.5 – OLEDB Database Classes

Class Description
OleDbConnection The physical database connection
OleDbCommand A command to execute on the database
OleDbDataAdapter Moves data between the DataSet and the physical Database using a connection and a command
OleDbDataReader Can iterate results returned from a command

Now that you have been introduced to the ADO.NET classes, it is time to write some real code. The example in the next section connects to the Microsoft Access Northwind Traders database using the OleDb classes.

Connecting to Access using OLE DB

In this section, we will use the Northwind Trader database that installs (optionally) with Microsoft Access. The program is a Console Application that simply loads a DataSet from the Customers table of the database. Then, we loop through the records, printing the CustomerName field to the screen.

Listing 15.2

// Example15_2.cs
using System;
using System.Data;
using System.Data.OleDb;

namespace csbook.ch15 {

class Example15_2 {

static void Main(string[] args) {
string conString =
@"Provider=Microsoft.JET.OLEDB.4.0;"
+ @"data source=c:\data\Northwind.mdb";


// create an open the connection
OleDbConnection conn = new OleDbConnection(conString);
conn.Open();

// create the DataSet
DataSet ds = new DataSet();

// create the adapter and fill the DataSet
OleDbDataAdapter adapter =
new OleDbDataAdapter("Select * from Customers", conn);
adapter.Fill(ds);

// close the connection
conn.Close();

DataTable dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows) {
Console.WriteLine(dr["CompanyName"].ToString());
}
}
}
}

The listing first creates a connection string that provides information to the OleDbConnection class. Specifically, the connection string contains the provider for the database engine that we want to connect to and the data source, which in this case is a Microsoft Access file. If the file were password protected, we would also specify a User ID and Password in the connection string. After creating the connection string, the program creates an OleDbConnection object, passing the connection string as the argument.

Next, the listing opens the database connection by calling the connection’s Open method. It also creates an empty DataSet that will later be filled from the database.

The OleDbAdapter class encapsulates the command to fill the DataSet. We create the adapter by passing a SQL query string and the database connection to its constructor. This query string will select all fields of all rows from the Customers table. The adapter’s Fill method will execute the query through the connection and load the results into the DataSet that we pass to it. The results include the data from the query as well as the metadata, defining its structure. When the call is complete, the DataSet will contain a table with all the Customer records from the database. At that point, we can close the database connection so that it can be used by some other application.

The tables within the DataSet are represented by the DataTable class. Each DataTable has a collection of DataRow objects storing the rows from the query. The foreach loop iterates through the DataRows, displaying the CompanyName field. The DataRow object uses the string "CompanyName" as an index to find the field of that name and retrieve its value. The value is returned as an object type. Calling the virtual ToString method will result in displaying the string value of the field.

Connecting to a SQL Server Database

The SQL Server versions of the connection, command, and adapter classes are listed below in Table 15.6. They are defined in the System.Data.SqlClient namespace.

Table 15.6 – SQL Server Database Classes

Class
Description
SqlConnection
The physical database connection
SqlDataCommand
A command to execute on the database
SqlDataAdapter
Moves data between the DataSet and the physical Database using a connection and a command
SqlDataReader
Can iterate results returned from a command

SQL Server also installs a copy of the Northwind Trader database. The next example is similar to the previous except that it uses the SQL Server versions of the connection and adapter.

While the previous listing used an adapter to execute a command internally and fill a DataSet, this example uses the command object directly. The program calls the SqlCommand object’s ExecuteReader method, which returns a SqlReader object. Then, it uses the SqlReader to loop through the records that have been returned.

Listing 15.3

// Example15_3.cs
using System;
using System.Data;
using System.Data.SqlClient;

namespace csbook.ch15 {

class Example15_3 {
static void Main(string[] args) {
// create an open the connection
SqlConnection conn =
new SqlConnection("Data Source=DESKTOP;"
+ "Initial Catalog=Northwind;"
+ "Persist Security Info=True;"
+ "User ID=jeff;Password=password");

conn.Open();

// create a SqlCommand object for this connection
SqlCommand command = conn.CreateCommand();
command.CommandText = "Select * from Customers";
command.CommandType = CommandType.Text;

// execute the command that returns a SqlDataReader
SqlDataReader reader = command.ExecuteReader();

// display the results
while (reader.Read()) {
string output = reader["CompanyName"].ToString();
Console.WriteLine(output);
}

// close the connection
reader.Close();
conn.Close();
}
}
}

Data Binding

Window controls have the ability to bind their properties to the properties of other objects. When two properties are bound, a change in one is reflected in the other.

For example, consider the following listing for the Student class. The Student has two properties—the string value Name and the double value Gpa.

class Student { private string name; private double gpa; public string Name { get { return name; } set { name = value; } } public double Gpa { get { return gpa; } set { gpa = value; } } }

If we declare an instance of the Student as a member of a Windows Form, we can bind the Name and Gpa properties to the Text properties of a pair of TextBoxes.

// create the student student = new Student(); student.Name = "Jeff"; student.Gpa = 4.0; // bind the properties textBox1.DataBindings.Add(new Binding("Text", student, "Name")); textBox2.DataBindings.Add(new Binding("Text", student, "Gpa"));

We can also put a Button on the Form that displays the current values of the student’s Name and Gpa. This allows us to view the changes made to the object after we change the text in the TextBox.

private void testButton_Click(object sender, EventArgs e) { MessageBox.Show("Name: " + student.Name + " GPA: " + student.Gpa); }

Running the example, we can see that when the form first opens the initial values are displayed.

Figure 15.2

Image:Figure15_2.jpg

Then, we can change the text in the text boxes and click the Test Button. The message box displays the values of the student object, which have been changed to the new values in the TextBoxes.

Figure 15.3

Image:Figure15_3.jpg

This type of data binding is called simple data binding. We can also bind a ListBox control to an array or collection of data. Binding to a collection of data is called complex data binding. To bind the ListBox to a collection you set the ListBox’s DataSource property. The ListBox will display the first public property of each item in the collection in its list. The next example binds an array of strings to a ListBox.

public partial class Form1 : Form { string[] names = { "Jeff", "Rachel", "Katy", "Evy", "Ben" }; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { listBox1.DataSource = names; } }

When the form is displayed, the names in the string array appear in the ListBox.

Figure 15.4

Image:Figure15_4.jpg



Using the DataGridView

The DataGridView is a control designed to be bound to database tables. You will find the DataGridView in the Data group on your Toolbox. In this section, we will use the DataGridView to build a SQL interface for the Northwind Trader database. This application is surprisingly simple using the classes provided by ADO.NET.

Figure 15.5

Image:Figure15_5.jpg

To create the user interface, place a TextBox on the form and set its Multiline property to true. Then, add two buttons, setting their Text properties to Query and Update. Finally, place a DataGridView component on the form. Your form might look something like the one in Figure 15.5.

Since we are going to connect to a Microsoft Access database, we need to use the OleDb classes. At the top of the file, you should add the following using statement to gain access to the System.Data.OleDb namespace.

using System.Data.OleDb;

The Form’s Load event should create an OleDbConnection object, an OleDbCommand object, and a DataSet object. Each of these objects should be stored as members of your Form class.

private void Form1_Load(object sender, EventArgs e) {
// connect to the database
string conString =
@"Provider=Microsoft.JET.OLEDB.4.0;"
+ @"data source=c:\data\Northwind.mdb";

// create an open the connection
conn = new OleDbConnection(conString);
command = conn.CreateCommand();

// create the DataSet
DataSet ds = new DataSet();
}

Next, you need to implement the select button’s Click handler. The method should start by clearing the DataSource property of the DataViewGrid and creating a fresh DataSet. Then, it should reopen the connection. Set the Command object’s CommandText property to the Text from the TextBox control. Then, create an adapter with the command and fill the new DataSet. Finally, set the DataGridView’s DataSource property to the first table in the DataSet’s Table collection.

private void button1_Click(object sender, EventArgs e) {
// clear the grids data source
dataGridView1.DataSource = null;

// create a new DataSet
ds = new DataSet();

// open the connection
conn.Open();

// run the query
command.CommandText = textBox1.Text;
adapter = new OleDbDataAdapter(command);
adapter.Fill(ds);

// close the connection
conn.Close();

// set the grid's data source
dataGridView1.DataSource = ds.Tables[0];
}

The last step is to implement the Click event handler for the Update button.

private void button2_Click(object sender, EventArgs e) {
// clear the grids data source
dataGridView1.DataSource = null;

// open the connection
conn.Open();

// run the query
command.CommandText = textBox1.Text;

int affected = command.ExecuteNonQuery();
MessageBox.Show ("There were " + affected + " rows affected");

// close the connection
conn.Close();
}

The update button sets the DataGridView’s DataSource property to null to clear the grid. Then, it reopens the connection and sets the command object’s CommandText property to whatever text is in the TextBox. This time the command is executed by the ExecuteNonQuery method. The ExecuteNonQuery method runs a command that updates or deletes records. It returns the number of rows affected by the command. Finally, the method displays a message box showing the number of rows affected and then closes the connection. Figure 15.6 shows a SELECT statement being executed on the database.

Figure 15.6

Image:Figure15_6.jpg

Figure 15.7 shows the update button being used to change the ContactTitle from Guru to Fool.

Figure 15.7

Image:Figure15_7.jpg

To verify that our update actually worked, we can run the query again.

Figure 15.8

Image:Figure15_8.jpg