This C# program demonstrates how to use row state filters in an SQL table in a database for Microsoft SQL in C#.
using System; using System.Data; using System.Data.SqlClient; namespace UsingRowStateFilters { class Program { static void Main(string[] args) { PrintFilteredRows("apostles"); } static void PrintFilteredRows(string sTableName) { // The using block ensures that the connection is closed when it exits this block. using (SqlConnection qConnection = new SqlConnection( @"Server=localhost\SQLEXPRESS01;Database=catholic;Trusted_Connection=True;")) { qConnection.Open(); DataSet qDataSet = new DataSet("catholic"); using (SqlDataAdapter qAdapter = new SqlDataAdapter( "SELECT * FROM " + sTableName, qConnection)) { qAdapter.Fill(qDataSet, sTableName); } DataTable qDataTable = qDataSet.Tables[sTableName]; DataView qDataView = new DataView(qDataTable); /// This is an array of the defined states. DataViewRowState[] qRowStates = new DataViewRowState[] { DataViewRowState.Added, DataViewRowState.CurrentRows, DataViewRowState.Deleted, DataViewRowState.ModifiedCurrent, DataViewRowState.ModifiedOriginal, DataViewRowState.None, DataViewRowState.OriginalRows, DataViewRowState.Unchanged }; // Run through the array of row states and apply a filter for each one. foreach (DataViewRowState eRowState in qRowStates) { Console.WriteLine(eRowState); Console.WriteLine("----------------------------------------"); qDataView.RowStateFilter = eRowState; // Iterate over the rows in the data table for (int i = 0; i < qDataView.Count; i++) { foreach (object qItem in qDataView[i].Row.ItemArray) { Console.Write(String.Format("{0,-20}", qItem)); } Console.WriteLine(); } Console.WriteLine(); } } } } }
Added ---------------------------------------- CurrentRows ---------------------------------------- John Chalice Andrew Transverse Cross Peter Keys Deleted ---------------------------------------- ModifiedCurrent ---------------------------------------- ModifiedOriginal ---------------------------------------- None ---------------------------------------- OriginalRows ---------------------------------------- John Chalice Andrew Transverse Cross Peter Keys Unchanged ---------------------------------------- John Chalice Andrew Transverse Cross Peter Keys Press any key to continue . . .
© 20072024 XoaX.net LLC. All rights reserved.