ADO.NET

ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples

Examples

Displaying the Row States in C#
Previous Home Next
private static void DisplayROWs(string , DataTable table)
{
Console.Clear();
Console.WriteLine("\n");
Console.WriteLine(s);
Console.WriteLine("-------------------------------------------");
foreach (DataRow dr in table.Rows)
{
Console.WriteLine(dr.RowState.ToString());
}
Console.WriteLine("\nPress Enter to Continue ..") ;
Console.Read() ;
}

Filling a DataTable and Displaying the Row States in C#

static void Main(string[] args)
{
using (SqlConnection scon = new SqlConnection(connectionString))
{
SqlCommand scmd = scon.CreateCommand();
scmd.CommandText = "Select * from student";
SqlDataAdapter sqlDa = new SqlDataAdapter(scmd);
DataTable StudentTable = new DataTable("student");
sqlDa.Fill(StudentTable);
DisplayRowStates(
"Row states for a freshly filled DataTable:",StudentTable);
}
}

 

Making Changes to the DataTable in C#

DataRow dr;
// Make Changes - Modify the puppy
dr = StudentTable.Rows[0];
dr["StudentName"] = "Anil";
// Make Changes - Delete the cat
dr = StudentTable.Rows[1];dr.Delete();
// Leave the Horse untouched.
// Make Changes - Insert a Record
dr = StudentTable.NewRow();
dr["StudentID"] = 4;dr["StudentName"] = "Ajay";
StudentTable.Rows.Add(dr);
DisplayRowStates("Row states for a modified DataTable:", StudentTable);

Setting Various Commands in C#

// Update the changes back to the database.
SqlCommandBuilder sqlcommr 
   = new SqlCommandBuilder(sqlDa);
// Setup Update Command
sqlDa.UpdateCommand = sqlcommr.GetUpdateCommand();
Console.WriteLine("Update Command: " 
   + sqlDa.UpdateCommand.CommandText);
// Setup Insert Command
sqlDa.InsertCommand = sqlcommr.GetInsertCommand();
Console.WriteLine("Insert Command: " 
   + sqlDa.InsertCommand.CommandText);
// Setup Delete Command
sqlDa.DeleteCommand = sqlcommr.GetDeleteCommand() ;
Console.WriteLine("Delete Command: " 
   + sqlDa.DeleteCommand.CommandText);
sqlDa.Update(animalsTable);
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: