for RuBoard |
This workshop will help reinforce the concepts covered in today's lesson.
1: | How does a data adapter know which rows have changes? |
A1: | A data adapter, or for that matter your code, can inspect the RowState property of the DataRow object to determine the current state of the row. Row states include Added , Deleted , Modified , and Unchanged . |
2: | How does a DataTable track the old values for a row? |
A2: | Each row in the DataTable can have up to three different copies or versions ( Current , Original , Proposed ) associated with it. The HasVersion method of the DataRow class is used to determine whether a particular version exists by accepting a value from the DataRowVersion enumeration. Various versions can be retrieved using the optional second argument of the Item property (the indexer in C#) of the DataRow . |
3: | What is the purpose of the GetChanges method? |
A3: | The GetChanges method can be used to return a DataTable or DataSet that contains only the rows that have been modified (their row state set to Added , Modified , or Deleted ). An overloaded signature also allows only changes from one or more of the row states to be returned. |
4: | What is the relationship between row states, row versions, and a DataView ? |
A4: | A DataView , through its RowStateFilter property, can be used to view a collection of rows from a DataTable that have a particular combination of RowState and version. For example, the RowFilter can be set to the ModifiedOriginal value from the DataRowViewState enumeration to show the Original values from rows with a row state of Modified . The DataView can then be bound to a control for display. |
Q1: | Today, write a console application that retrieves Reviews from the ComputeBooks database. Then programmatically edit the first row using the BeginEdit and EndEdit methods . Check the RowState property and HasVersion method before and after calling EndEdit to see how the values change. Commit the changes to the row after it has been modified. |
A1: | One possible solution is as follows : static void Main(string[] args) { SqlConnection con = new SqlConnection( "server=ssosa;database=compubooks;trusted_connection=yes"); SqlDataAdapter da = new SqlDataAdapter("usp_GetReviews",con); DataTable reviews = new DataTable("Reviews"); da.SelectCommand.CommandType = CommandType.StoredProcedure; da.SelectCommand.Parameters.Add( new SqlParameter("@isbn",SqlDbType.NChar,10)); da.SelectCommand.Parameters[0].Value = "06720083X"; // Get the data try { da.Fill(reviews); } catch (SqlException e) { Console.WriteLine(e.Message); } // Edit the row reviews.Rows[0].BeginEdit(); reviews.Rows[0]["Stars"] = 4; CheckIt(reviews.Rows[0]); reviews.Rows[0].EndEdit(); CheckIt(reviews.Rows[0]); // Commit the changes reviews.Rows[0].AcceptChanges(); } static void CheckIt(DataRow r) { Console.WriteLine(r.RowState.ToString()); Console.WriteLine(r.HasVersion(DataRowVersion.Current).ToString()); Console.WriteLine(r.HasVersion(DataRowVersion.Default).ToString()); Console.WriteLine(r.HasVersion(DataRowVersion.Original).ToString()); Console.WriteLine(r.HasVersion(DataRowVersion.Proposed).ToString()); } In this solution, the Main method of the console application calls the usp_GetReviews stored procedure and passes in an ISBN to retrieve the reviews into a DataTable . The first row of the table is then edited and its RowState and row versions printed by the CheckIt method. The row is then committed using the AcceptChanges method. |
for RuBoard |