To visually create a data set, you have two options. You can click the DataSet object from the Data section of the Toolbox and click the form. You would specify it as an Untyped Dataset and click OK. After manually creating the data set, you must fill it with records to make it useful. As a second option to visually create a data set, add a new data source from either the main menu (PROJECT -> Add New Data Source) or from the Data Sources window. Then use the Data Source Configuration Wizard. When the wizard ends, a class is generated. The class is derived from the DataSet class and holds the name you had specified for the data set in the last page of the wizard. The class may start as follows: public partial class dsLambdaSquareApartments : global::System.Data.DataSet
{
}
After creating the data set, you can use it, along with its table(s) (DataTable objects), its (their) columns (DataColumn objects), and its (their) records (DataRow objects and DataRowCollection lists). Because a data set is tied to the database, it provides all of its services. This means that a data set can be used for any necessary maintenance assignment. There are many ways you can perform maintenance on a data set, a table, a column, or a record. Each one of these items is represented by one or more classes and those classes support various types of maintenance operations. Besides the means provided by the data set, the tables, their columns, and their records, the table adapter generated by the wizard is equipped with various methods.
Although the records of a database belong to a table, if you want to use them in an external application, you can save them in an XML file. To support this, the DataSet class is equipped with the WriteXml() method. Here is an example of calling it: private void btnSave_Click(object sender, EventArgs e)
{
using (SqlConnection scnDepartmentStore =
new SqlConnection("Data Source=(local);" +
"Database='DepartmentStore1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdStoreItems =
new SqlCommand("SELECT * FROM StoreItems; ",
scnDepartmentStore);
SqlDataAdapter sdaStoreItems = new SqlDataAdapter();
DataSet dsStoreItems = new DataSet("StoreItems");
scnDepartmentStore.Open();
sdaStoreItems.SelectCommand = cmdStoreItems;
sdaStoreItems.Fill(dsStoreItems);
dsStoreItems.WriteXml(@"C:\Exercise\StoreItems1.xml");
}
}
The tables, views, and/or stored procedures of a DataSet object are stored in the DataSet.Tables property that is of type DataTableCollection. After filling up a DataSet object, if the selection statement of the data adapter includes only one table, view, or stored procedure, the first table, view, or stored procedure of the statement can be identified with the index of 0 as in DataTableCollection[0]. If the statement includes only one table, view, or stored procedure, only a 0 index can be used. As the DataTableCollection[0] value allows you to identify a table, view, or stored procedure, you can retrieve any table-related information with this information. For example, you can get the object name of the table, view, or stored procedure and specify it as the DataMember property of a DataGridView control. Here is an example: private void Form1_Load(object sender, System.EventArgs e)
{
SqlConnection cnnVideos = new SqlConnection(
"Data Source=(local);Database='VideoCollection';Integrated Security=yes");
string strVideos = "SELECT ALL * FROM Videos;";
SqlCommand cmdVideos = new SqlCommand(strVideos, cnnVideos);
cnnVideos.Open();
SqlDataAdapter dadVideoCollection = new SqlDataAdapter(cmdVideos);
DataSet setVideos = new DataSet("VideoCollection");
dadVideoCollection.Fill(setVideos);
dataGridView1.DataSource = setVideos;
dataGridView1.DataMember = setVideos.Tables[0].TableName;
cnnVideos.Close();
}
Just as you can use the filled DataSet object to locate a table, view, or stored procedure by its index, inside of the identified table, view, or stored procedure, you can also locate a particular column you need. As you may know already, the columns of a table, view, or stored procedure are stored in the Columns property of a DataTable object and the Columns property is of type DataColumnCollection. Each column inside of the table can be identified by its index. The first column has an index of 0. The second column has an index of 1, and so on. Once you have identified a column, you can manipulate it as you see fit. In the following example, since we (behave like we) don't know the name of the second column, a message box displays that information for us: private void Form1_Load(object sender, System.EventArgs e)
{
SqlConnection cnnVideos = new SqlConnection(
"Data Source=(local);Database='VideoCollection';Integrated Security=yes");
string strVideos = "Blah Blah Blah";
SqlCommand cmdVideos = new SqlCommand(strVideos, cnnVideos);
cnnVideos.Open();
SqlDataAdapter dadVideoCollection = new SqlDataAdapter(cmdVideos);
DataSet setVideos = new DataSet("VideoCollection");
dadVideoCollection.Fill(setVideos);
dataGrid1.DataSource = setVideos;
dataGrid1.DataMember = setVideos.Tables[0].TableName;
DataColumn colSecond = setVideos.Tables[0].Columns[1];
MessageBox.Show("The name of the second column is " + colSecond.ColumnName);
cnnVideos.Close();
}
When visiting the records of a table using a form of your application, if you provide the means for the user to move from one record to another, if the user gets to a record and changes something in it, that record would not be automatically updated when the user moves to another record. To update a record using the data adapter, (we already saw that) the SqlDataAdapter class inherits the Update() method from its parent the DbDataAdapter. The Update() method is overloaded with various versions. One of its versions uses the following syntax: public override int Update(DataSet dataSet); This version takes a DataSet object as argument. This means that the data adapter would read the information stored in the DataSet object and update the database with it. This is probably one of the easiest or fastest means of updating data of a table.
After filling out a DataSet object with information from a data adapter, the records of the table(s) included in the selection statement become available from the DataSet object. As you may know already, the records of a table, view, or stored procedure are stored in the Rows property of the DataTable object. Data entry with a data adapter is performed just a few steps once you have properly bound the controls of your form to a DataSet object. To start, you can access the form's BindingContext property to get its BindingContext.Item property. The second version of this property allows you to specify the data source and the table name. After specifying the DataSet object that holds the records and the table that holds the data, you can first call the EndCurrentEdit() method to suspend any record editing that was going on. After this, call the AddNew() method to get the table ready for a new record. This allows the user to enter values in the Windows control.
Some of the operations you can perform on a data set include copying a table or the entire data set by calling the appropriate Copy() method (DataTable.Copy() or DataSet.Copy() respectively). To get the number of records in a table, access the desired table (using its name or its index) from the data set that was generated, access its Rows property, and access its Count property.
We saw that, when a table adapater has been created, its class is equipped with a method named Fill that is used to fill a data set. You too, at times, will want to fill or refill a table with records from its corresponding data table. To do this, access your table adapter, call its Fill() method, and pass the table as argument. Here is an example: private void btnEmployees_Click(object sender, EventArgs e)
{
taEmployees.Fill(dsFunDS1.Employees);
}
Editing a record consists of changing one or more of its values. To programmatically do this, you must first locate and open the record, then change the necessary value(s). After doing this, if you want to apply the change(s) to the table, you must update it. To assist you with this, the generated table adapter is equipped with the Update() method. This method is overloaded with four versions: one for a data set, one for a data table, one for a record (a data row), and one for an array of records (a DataRow[] array). Therefore, after making the changes on either a record, some records, or a table, call the appropriate version of the method to apply the changes.
One of the most fundamental operations you can perform on a data set consists of creating a new record. To assist you with the tables, their columns and records, the data set class that the wizard generates inherits the properties and methods of the DataSet class. This includes the Tables property. You can use this property to access a table, view, or stored procedure, based on its name or its index. Once you have obtained the table, you can perform any normal operation you want. To support record creation, we already know that the DataTable class is equipped with the NewRow() method. To use this method, access the data set object that was generated for you, access the desired table, and call this method. After calling the DataTable.NewRow() method, you can access each column by its name or its index and assign the desired value to it. You can access the columns in any order of your choice. You can choose what columns to provide values for and which ones to ignore. When doing this, you must observe the rules established in the table's structure:
After specifying the value(s) of column(s), to apply them to the table, call the Add() method of the Rows property of the table. After calling the DataRowCollection.Rows.Add() method, you must update the table adapter. Here is an example: private void btnAddNewRecord_Click(object sender, EventArgs e)
{
DataRow customer = dsCeilInn1.Tables["Customers"].NewRow();
customer["AccountNumber"] = "955740";
customer["FullName"] = "Albert Rhoads";
customer["PhoneNumber"] = "116-917-3974";
customer["EmergencyName"] = "Jasmine";
customer["EmergencyPhone"] = "Rhoads";
dsCeilInn1.Tables["Customers"].Rows.Add(customer);
taCustomers.Update(customer);
}
In the same way, you can use these steps to add as many records as you want. Instead of adding one record at a time, you can store the records in an array and add them at once, as a block. This is possible because the DataTable.Rows property, which is of type DataRowCollection, is equipped with the ItemArray property. After adding the record(s) to the table, you must update the data set. To assist you with this, the generated table adapter is equipped with a method named Update. After the new record has been added, it is marked with the RowState.Added value.
Although you can use Transact-SQL to find a record, the data set provides its own mechanism through the DataRowCollection class that is represented in a table with the Rows property. You can first use the DataRowCollection[] (actually DataRowCollection.Item[]) property to locate a record. Once you have the record, you can use the DataRow[] array (or collection) to identify a column and inquire about its value. If the result is not null, a record is found. If a value is not found, the compiler may throw an IndexOutOfRangeException exception. You can use this exception to find out whether a record was found or not. Because the DataRow.Item property is overloaded, you can access a column by its index inside the table or using its actual name.
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
After locating a record, you can perform an action on it. One of the things you can do is to delete a record. To support this operation, the DataRow class is equipped with the Delete() method. Therefore, to delete a record, first find it. To assist you with this, the DataRowCollection class, which is represented in a table by the Rows property, is equipped with the Find() method. After finding the record, call its DataRow.Delete() method. After deleting the record, you must update the table by calling the Update() method of the generated table adapter.
A table adapter, in combination with its parent data set, provides many options to perform data analysis. You can use:
The string class provides tremendous opportunities for data analysis through its built-in methods. It gives the ability to get a list of records that start, or end, with a certain character or a combination of characters, to get the records that contain a certain word, etc. In the Data section of the Toolbox, Microsoft Visual Studio provides a component you can use to analyze, filter, or sort records. To use it, click the BindingSource object and click the form. You should then specify the DataSource as the data set object you had added to your form. You should also specify its DataMember as the table on which you will operate. To perform data analysis using a binding source, you use the Filter property of the BindingSource class. You can enter an expression in the Properties window or type one when you are ready to apply the filter. Here is an example: private void btnFind_Click(object sender, EventArgs e)
{
if (cbxOperators.Text.Equals("Equal To"))
bsEmployees.Filter = cbxColumns.Text + " = '" + txtCriterion.Text + "'";
if (cbxOperators.Text.Equals("Different From"))
bsEmployees.Filter = cbxColumns.Text + " <> '" + txtCriterion.Text + "'";
if (cbxOperators.Text.Equals("Starts With"))
bsEmployees.Filter = cbxColumns.Text + " LIKE '" + txtCriterion.Text + "%'";
if (cbxOperators.Text.Equals("Doesn't Start With"))
bsEmployees.Filter = cbxColumns.Text + " NOT LIKE '" + txtCriterion.Text + "%'";
if (cbxOperators.Text.Equals("Contains"))
bsEmployees.Filter = cbxColumns.Text + " LIKE '%" + txtCriterion.Text + "%'";
if (cbxOperators.Text.Equals("Doesn't Contain"))
bsEmployees.Filter = cbxColumns.Text + " NOT LIKE '%" + txtCriterion.Text + "%'";
if (cbxOperators.Text.Equals("Ends With"))
bsEmployees.Filter = cbxColumns.Text + " LIKE '%" + txtCriterion.Text + "'";
if (cbxOperators.Text.Equals("Doesn't End With"))
bsEmployees.Filter = cbxColumns.Text + " NOT LIKE '%" + txtCriterion.Text + "'";
taEmployees.Fill(dsDepartmentStore1.Employees);
dgvEmployees.DataSource = bsEmployees;
}
The BindingSource.Filter property supports all types of data analysis operators of the Transact-SQL language. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||