This C# program demonstrates how to read the row states in an SQL table in a database for Microsoft SQL in C#.
using System;
using System.Data;
using System.Data.SqlClient;
namespace ReadRowState {
class Program {
static void Main(string[] args) {
Console.WriteLine(ReadRowStates());
}
public static string ReadRowStates() {
// 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 first row
qRowCollection[0]["symbol"] = "Two Keys";
// Add in a new row
DataRow qNewRow = qNewTable.NewRow();
qNewRow["name"] = "Philip";
qNewRow["symbol"] = "Latin Cross";
qRowCollection.Add(qNewRow);
string sRowStates = "";
// 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) {
sRowStates += String.Format("{0,-20}", qData);
}
sRowStates += "--> " + qRow.RowState + "\n";
}
return sRowStates;
}
}
}
}
John Two Keys --> Modified Andrew Transverse Cross --> Unchanged Peter Keys --> Unchanged Philip Latin Cross --> Added Press any key to continue . . .
© 20072025 XoaX.net LLC. All rights reserved.