This C# program demonstrates how to check the DataRow version in an SQL table in a database for Microsoft SQL in C#.
using System;
using System.Data;
using System.Data.SqlClient;
namespace CheckDataRowVersion {
class Program {
static void Main(string[] args) {
Console.WriteLine(CheckRowVerions());
}
public static string CheckRowVerions() {
// 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;")) {
DataTable qNewTable = new DataTable();
qNewTable.Clear();
// Use an adapter to read the table into the DataTable object
using (var qSqlAdapter = new SqlDataAdapter("SELECT * FROM apostles", qConnection)) {
qSqlAdapter.Fill(qNewTable);
}
DataRowCollection qRowCollection = qNewTable.Rows;
// Modify the third row. This does not change the version.
qRowCollection[2]["symbol"] = "Two Keys";
// Add in a new row
DataRow qNewRow = qNewTable.NewRow();
qNewRow["name"] = "Philip";
qNewRow["symbol"] = "Latin Cross";
qRowCollection.Add(qNewRow);
string sRowVerions = "";
// Create an array of the row version specifiers.
DataRowVersion[] qaVersions = new DataRowVersion[] {
DataRowVersion.Current,
DataRowVersion.Default,
DataRowVersion.Original,
DataRowVersion.Proposed,
};
// Run through the rows of the table to get the row state for each
foreach (DataRow qRow in qRowCollection) {
foreach (object qData in qRow.ItemArray) {
sRowVerions += String.Format("{0,-20}", qData);
}
// Delete the second row after we access it.
if (qRow == qRowCollection[1]) {
qRow.Delete();
}
sRowVerions += "\n";
foreach (DataRowVersion eVersion in qaVersions) {
sRowVerions += eVersion + " = " + qRow.HasVersion(eVersion) + "\n";
}
}
return sRowVerions;
}
}
}
}
John Chalice Current = True Default = True Original = True Proposed = False Andrew Transverse Cross Current = False Default = False Original = True Proposed = False Peter Two Keys Current = True Default = True Original = True Proposed = False Philip Latin Cross Current = True Default = True Original = False Proposed = False Press any key to continue . . .
© 20072025 XoaX.net LLC. All rights reserved.